javascript - 如何确保 ES3 程序在 ES5 引擎中运行?

标签 javascript backwards-compatibility ecma262 ecmascript-5

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


示例:

Many articles已写明 this === null ||这个 === undefined 是可能的 在 ES5 严格模式下:

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

但是,什么the standard 真的表明 ES5 引擎也允许在非严格模式下:

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 的浏览器,事实证明这可能 break current scripts .太好了。


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

那么确保我们久经考验的 ES3 脚本继续完美运行的最佳方法是什么?某种自动化测试套件?我们是否必须全部手动测试?

最佳答案

郑重声明,提问者对ES5 15.3.4.3的解释是错误的。在 ES5 中对非严格函数的任何调用在观察上应该与在 ES3 中相同。全局对象仍然传递给任何以 null 或 undefined 作为 this 值调用的非严格函数。

分析缺失的部分是10.4.3“输入函数代码”:

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. 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 或未定义的 this 值。 ES5 指定被调用方具有该责任(如果它不是严格模式函数)。对于非严格代码,这不是可观察到的差异。只有当被调用方是严格函数时,规范更改才会有所不同。

关于javascript - 如何确保 ES3 程序在 ES5 引擎中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3996906/

相关文章:

php - 如何使用 Zend_Date 简单地更改时区偏移量?

javascript - FullCalendar 按钮放置月份名称应位于左右箭头之间

javascript - ECMAScript 规范 v5 中缺少有关输入在全局作用域中声明的函数时会发生什么的详细信息?

javascript - 保存在 JavaScript 中的命名函数表达式中标识符的不可变绑定(bind)记录在哪里?

javascript - 符合标准的权威 JavaScript 验证

javascript - polymer 升级后,当作为 HTML 导入加载时,纸质复选框会创建两次

javascript - Ajax成功函数无法在自定义函数内访问

ios - 支持 iOS 6 和 7

.net - DataContractSerializer:为什么不删除成员?

android - 如何在 Android SDK 中编写向后兼容的新功能?