如何确保ES3程序将在ES5引擎中运行? [英] How to make sure ES3 programs will run in an ES5 engine?

查看:102
本文介绍了如何确保ES3程序将在ES5引擎中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此ECMAScript 5引入了与ECMAScript 3的一些不兼容性.

So ECMAScript 5 introduces some incompatibilities with ECMAScript 3.

示例:

很多

Many articles have been written stating that this === null || this === undefined is possible in ES5 strict mode:

"use strict";
(function () {
    alert(this); // null
}).call(null);

但是,标准 确实是暗示ES5引擎也允许此在非严格模式下

But, what the standard really suggests is that ES5 engines also allow this in non-strict mode:

15.3.4.3 ... 将thisArg值作为this值不加修改地传递.这是对版本3的更改,版本3将undefinednull thisArg替换为全局对象,并将ToObject应用于所有其他值,并将结果作为this值传递.

15.3.4.3 ... The thisArg value is passed without modification as the this value. This is a change from Edition 3, where a undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value.

当前,IE9是唯一实际以这种方式实现ES5的浏览器,事实证明,这可能

Currently, IE9 is the only browser that actually implements ES5 this way, and it turns out that this may break current scripts. Great.

ES5规范的附录E 列出了许多其他不兼容性

Annix E of ES5 spec lists dozens of other incompatibilities.

那么,确保我们久经考验的ES3脚本能够继续正常运行的最佳方法是什么?某种自动测试套件?我们需要手动测试吗?

So what's the best way to make sure that our well-tried ES3 scripts will continue running flawlessly? Some kind of automated test-suite? Will we have to test it all manually?

推荐答案

根据记录,发问者对ES5 15.3.4.3的解释不正确.可以看出,在ES5中对非严格函数的任何调用都应与ES3中的相同.全局对象仍会传递到任何以null或undefined作为this值调用的非严格函数中.

For the record, the questioner's interpretation of ES5 15.3.4.3 is incorrect. Any call to a non-strict function in ES5 should be observably the same as in ES3. The global object is still passed into any non-strict function that is called with null or undefined as the this value.

分析的缺失部分是10.4.3输入功能代码":

The missing piece of the analysis is 10.4.3 "Entering Function Code":

当控制进入执行状态时,执行以下步骤 功能对象 F 中包含的功能代码的上下文,调用者 提供了 thisArg ,调用者提供了 argumentsList :

The following steps are performed when control enters the execution context for function code contained in a function object F, a caller provided thisArg, and a caller provided argumentsList:

  1. 如果功能代码是严格代码,请将ThisBinding设置为 thisArg .
  2. 否则,如果 thisArg 未定义,则设置ThisBinding 到全局对象.
  3. ...
  1. If the function code is strict code, set the ThisBinding to thisArg.
  2. Else if thisArg is null or undefined, set the ThisBinding to the global object.
  3. ...

ES3指定调用者负责将全局对象替换为null或未定义的该值. ES5指定被叫方具有该职责(如果它不是严格模式功能).对于非严格代码,这不是可观察到的差异.仅当被调用方是严格功能时,规范更改才有所不同.

ES3 specified that the caller was responsible for substituting the global object for a null or undefined this value. ES5 specifies that the callee has that responsiblity (if it isn't a strict mode function). For non-strict code this is not an observable difference. The specification change only makes a difference when the callee is a strict function.

这篇关于如何确保ES3程序将在ES5引擎中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆