JScript,JavaScript和ECMA Script之间的功能区别是什么?

问题描述:

常见问题

首先,我知道ECMA Script是标准的,而JavaScript和JScript是实现。我知道所有这三个规范都有自己的规格,并且引擎,解释器和实现也很多,但是我的具体问题是:

To start, I know ECMA Script is the standard, and JavaScript and JScript are implementations. I understand that all three have their own specifications maintained, and there are many, many engines, interpreters, and implementations, but my specific question is:

假设a的实现对于这三者中的每一个来说,它都是完美的解释器和引擎,那么您可以在一个中做什么而又不能在另一个中做,或者在一个中产生的效果与其他两个不一样?

Assuming the implementation of a perfect interpreter and engine for each of the three, what could you do in one that you could not do in another, or what would have different effects in one than the other two?

我理解这是一个广泛的问题,但是由于两种语言(JScript和JavaScript)均源自规范(ECMAScript),因此实际差异应该可以忽略不计。

I understand it's a broad question, but as both languages (JScript & JavaScript) are derived from the specification (ECMAScript), the practical differences should be negligible.

再说一次,我不是在谈论跨浏览器的兼容性(IE8和IE9使用了不同的引擎,这些引擎对JScript的解释不同,并且标准随时间而改变),而是纯ECMA5,JavaScript(如果有正式标准,我想是最接近的是 W3C MDN 和JScript(显然维护在 MSDN (见图))。

Again, I'm not talking about cross-browser compatibility (IE8 & IE9 used different engines that interepreted JScript differently, and standards have changed over time), but pure ECMA5, JavaScript (if there is an official standard, I guess the closest is W3C or maybe MDN, and JScript (which is apparently maintained at MSDN (go figure)).

注意:

这与此问题已经过时五年,并且处理术语的定义,而不是语言的应用,或这个问题,再次说明JavaScript和JScript是ECMAScript的方言,但没有任何功能上的差异。

This is not a duplicate of this question which is five years out of date, and deals with the definition of the terms, not the applications of the languages, or this question which again explains that JavaScript and JScript are dialects of ECMAScript, but does not go into any functional differences.

这个问题是最接近的,但具体来说是我米之后是技术陷阱操作员期望X和获得Y应该保持警惕。一个很好的例子是来自此问题,其中以下代码:

This question is closest, but specifically what I'm after are technical pitfalls a developer expecting X and getting Y should be wary of. A good example would be from this question where the following code:

// just normal, casual null hanging out in the sun
var nullA = null;
// query for non existing element, should get null, same behaviour also for getElementById
var nullB = document.querySelector('asdfasfdf');

// they are equal
console.log(nullA === nullB);

// false
nullA instanceof Object;

// will throw 'Object expected' error in ie8. Black magic
nullB instanceof Object;

在JScript的实现上表现出差异,理论上不符合ECMA标准。

showed a difference in implementations of JScript, that did not in theory comply with ECMA Standards.

EMCAScript标准的实现不仅仅是使规范规则生效的代码。 ECMAScript标准是故意不完整的:

A implementation of the EMCAScript standard is more than code that brings the specification rules to life. The ECMAScript standard is deliberately incomplete:


每个支持ECMAScript的Web浏览器和服务器都提供自己的宿主环境,从而完善了ECMAScript执行环境。 / p>

Each Web browser and server that supports ECMAScript supplies its own host environment, completing the ECMAScript execution environment.

ECMAScript实现必须提供主机环境。对于Web浏览器,该主机环境包括DOM操作API和W3C和WHATWG指定的其他API。 ECMAScript没有指定这些API的行为(实际上是它们的存在)。

An ECMAScript implementation must supply a "host environment". In the case of a Web browser, that host environment includes DOM manipulation APIs and other APIs specified by the W3C and WHATWG. The behaviors of (indeed, the existence of) these APIs are not specified by ECMAScript.

用于完成以下对象的主机环境的对象一个实现称为宿主对象。主机对象不一定要遵循正常的对象规则:它们可能会引发对属性访问的错误,这些错误在本机(非主机)对象上有效,或者它们可能允许某些本来不允许的操作。

Objects used to complete the "host environment" of an implementation are called "host objects". Host objects are not bound to follow normal object rules: they may throw errors for property access that would be valid on a native (non-host) object, or they might allow certain actions that would be natively disallowed.

JScript和JavaScript可能以不同方式实现其DOM API。在某些特定点上哪种实现是正确的,与ECMAScript的合规性无关,而与W3C标准的合规性有关。即使DOM对象似乎表现出某些与正常 ECMAScript行为相反的行为(例如您的 instanceof 错误示例),根据第8.6.2节

JScript and JavaScript might implement their DOM APIs differently. Which implementation is "correct" on some particular point is not a matter of ECMAScript compliance, but rather a matter of compliance with W3C standards. Even if a DOM object seems to exhibit some behavior that runs contrary to "normal" ECMAScript behaviors (like your instanceof error example), it's still legal ECMAScript according to section 8.6.2:


只要与本文档中所述的特定宿主对象限制相一致,宿主对象就可以通过任何依赖于实现的行为来支持这些内部属性。

Host objects may support these internal properties with any implementation-dependent behaviour as long as it is consistent with the specific host object restrictions stated in this document.

此处的内部属性包括逻辑操作,例如按名称获取对象属性的值,编码为 [[Get]] 。主机对象的自定义 [[Get]] 实现可能会引发错误,或者忽略先前设置的属性。

"Internal properties" here include logical operations like "getting the value of an object's property by name", codified as [[Get]]. A host object's custom [[Get]] implementation might throw an error, or ignore a previously-set property.

API差异与实际的语言差异不同。 语言的差异表明所支持的词汇语法或本机(非宿主)对象的行为有所不同。实际语言中的一些差异包括:

API differences are distinct from actual language differences. A language difference suggests a difference either in supported lexical grammar or in the behavior of native (non-host) objects. Some differences in actual language include:


  • JScript允许 cc_on 引起条件编译

  • Mozilla的JavaScript支持 yield 关键字,生成器以及许多ES5规范以外的其他内容(但可能会出现在ES6中)

  • 所有浏览器都支持块形式的函数声明,这不是合法的ECMAScript语法:

  • JScript allows for cc_on comments that cause conditional compilation
  • Mozilla's JavaScript supports the yield keyword, and generators, and a bunch of other stuff that is not in the ES5 spec (but likely will be in ES6)
  • All browsers support function declarations in blocks, which is not legal ECMAScript syntax:

function foo() {
    bar();

    if(condition) {
        function bar() { } // this is not legal
    }
}

支持此功能的浏览器(即所有'em)正在扩展ECMAScript语言。此外,JScript将提升 在该示例中,而Mozilla的JavaScript则不会。这是因为两个浏览器都以不兼容的方式扩展了ECMAScript语言。

Browsers that support this (i.e., all of 'em) are extending the ECMAScript language. Also, JScript will hoist bar in that example, while Mozilla's JavaScript will not. This is because the two browsers have extended the ECMAScript language in incompatible ways.