简体   繁体   English

新的ECMAScript 5函数有哪些现代化程序脚本?

[英]What modernizer scripts exist for the new ECMAScript 5 functions?

ECMAScript 5 has quite a few nice additions. ECMAScript 5有很多不错的补充。 John Resig has a good overview here . John Resig在这里有一个很好的概述 Here is a good ECMAScript 5 compatibility table . 这是一个很好的ECMAScript 5兼容性表

A lot of this stuff can be "faked" for browsers that don't support these functions yet. 对于那些不支持这些功能的浏览器,很多这样的东西都可以被“伪造”。 Do you know of any scripts that can do this? 你知道任何可以做到这一点的脚本吗? I'm particularly interested in Object.create. 我对Object.create特别感兴趣。

For example, Douglas Crockford's JSON script checks if JSON functions exist before creating them. 例如, Douglas Crockford的JSON脚本在创建JSON函数之前检查它们是否存在。

If there was more like the JSON one we could include them when we need to use the new functions. 如果有更多像JSON那样我们可以在需要使用新函数时包含它们。

Crockford recommends this kind of Object.create shim: Crockford推荐这种Object.create垫片:

if (typeof Object.create != "function") {
  Object.create = function (o) {
    function F(){}
    F.prototype = o;
    return new F;
  };
}

But please don't do this . 但请不要这样做

The problem with this approach is that ES5 Object.create has a signature of 2 arguments : first — an object to inherit from, and second (optional) — an object representing properties (or rather, descriptors) to add to newly created object. 这种方法的问题在于ES5 Object.create具有2个参数的签名:第一个 - 要继承的对象,第二个(可选) - 表示要添加到新创建的对象的属性(或更确切地说,描述符)的对象。

Object.create(O[, Properties]); // see 15.2.3.5, ECMA-262 5th ed.

What we have is an inconsistent implementation with 2 different behaviors . 我们所拥有的是具有2种不同行为不一致实现 In environments with native Object.create , method knows how to handle second argument; 在具有本机Object.create环境中,方法知道如何处理第二个参数; in environments without native Object.create , it doesn't. 在没有本机Object.create环境中,它没有。

What are the practical implications? 有什么实际意义?

Well, if there's some code (say, a third party script) that wants to use Object.create , it's rather reasonable for that code to do this: 好吧,如果有一些想要使用Object.create的代码(比方说,第三方脚本),那么代码执行此操作是相当合理的:

if (Object.create) {
  var child = Object.create(parent, properties);
}

— essentially assuming that if Object.create exists, it must conform to specs — accept second argument and add corresponding properties to an object. - 基本上假设如果Object.create存在,它必须符合specs - 接受第二个参数并将相应的属性添加到对象。

But, with the above-mentioned shim, second argument is simply ignored. 但是,对于上述垫片,第二个论点被忽略了。 There's not even an indication of something going 甚至没有迹象表明事情会发生 wrong 错误 differently. 不同。 A silent failure, so to speak — something that's rather painful to detect and fix. 可以说是一种无声的失败 - 一种在检测和修复方面相当痛苦的事情。

Can we do better? 我们可以做得更好吗?

Well, it's actually impossible to create a fully-conforming Object.create shim using only (standard) ES3 facilities . 好吧,实际上不可能仅使用(标准)ES3工具创建完全符合的Object.create垫片 The best solution is to create a custom wrapper method. 最好的解决方案是创建自定义包装器方法。

There are, however, few alternative (less than optimal) things you can try: 但是,您可以尝试很少的替代(不太理想)的事情:

1) Notify user about inability to work with second argument 1)通知用户无法使用第二个参数

if (!Object.create) {
  Object.create = function (o) {
    if (arguments.length > 1) { 
      throw Error('second argument is not supported'); 
    }
    // ... proceed ...
  };
}

2) Try to handle second argument: 2)尝试处理第二个参数:

if (!Object.create) {
  Object.create = function (parent, properties) {
    function F(){}
    F.prototype = parent;
    var obj = new F;
    if (properties) {
      // ... augment obj ...
    }
    return obj;
  }; 
}

Note that "properties" is an object representing property descriptors , not just property names/values, and is something that's not very trivial to support (some things are not even possible, such as controlling enumerability of a property): 请注意,“properties”是一个表示属性描述符的对象,而不仅仅是属性名称/值,并且是一些不太容易支持的东西(有些东西甚至不可能,例如控制属性的可枚举性):

Object.create(parent, {
  foo: {
    value: 'bar',
    writable: true
  },
  baz: {
    get: function(){ return 'baz getter'; },
    set: function(value){ return 'baz setter'; },
    enumerable: true
  }
});

The other inconsistency in the original shim is that it doesn't take care of parent object being null . 原始填充程序中的另一个不一致是它不处理父对象为null

var foo = Object.create(null);

This creates an object whose [[Prototype]] is null ; 这会创建一个[[Prototype]]为null ; in other words, object that doesn't inherit from anything, not even Object.prototype (which all native objects in ECMAScript inherit from). 换句话说,对象不从任何东西继承,甚至不是Object.prototype (ECMAScript中的所有本机对象都继承自)。

foo.toString; // undefined
foo.constructor; // undefined
// etc.

This is, by the way, useful to create "proper" hash tables in ECMAScript. 顺便说一句,这对于在ECMAScript中创建“适当的”哈希表很有用。

It's possible to emulate this behavior, but only using non-standard extensions, such as "magical" __proto__ property (so implementation would be not very portable or robust). 可以模拟这种行为,但只能使用非标准扩展,例如“魔法” __proto__属性(因此实现不是非常便携或健壮)。 Solution to this problem is similar: either emulate ES5 implementation fully, or notify about inconsistency/failure. 此问题的解决方案类似:要么完全模拟ES5实现,要么通知有关不一致/失败的问题。

es5-shim http://github.com/kriskowal/es5-shim/ es5-shim http://github.com/kriskowal/es5-shim/

This was part of the narwhal stand-alone javascript environment, but has been broken out on its own. 这是独角鲸独立的javascript环境的一部分,但它已经自行分解。 It's pretty darn mature and precise. 它非常成熟和精确。

es5 - JavaScript/EcmaScript 5 in 3 is a collection shared at BitBucket. es5 - JavaScript / EcmaScript 5 in 3是BitBucket共享的集合。 Object.create in particular is an easy one to fake , made popular by Crockford et al, but improved on here by Justin Love , focussing on many ES5 parts. 特别是Object.create是一个容易伪造的东西受到Crockford等人的欢迎,但Justin Love在这里进行了改进,专注于许多ES5部件。

If you don't mind learning the library and writing some code yourself, you can find some code implementations of the ECMAScript 5 library at 如果您不介意学习库并自己编写代码,可以在以下位置找到ECMAScript 5库的一些代码实现

https://developer.mozilla.org/En/JavaScript/ECMAScript_5_support_in_Mozilla https://developer.mozilla.org/En/JavaScript/ECMAScript_5_support_in_Mozilla

For example, the code for Array.filter 例如, Array.filter的代码

And then Crockford has JSON.parse/stringify in json2.js 然后Crockford在json2.js中有JSON.parse / stringify

https://github.com/douglascrockford/JSON-js https://github.com/douglascrockford/JSON-js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM