新的 ECMAScript 5 函数存在哪些现代化脚本?
ECMAScript 5 有很多不错的补充。 John Resig 在此提供了很好的概述。这是一个很好的 ECMAScript 5 兼容性表。
对于尚不支持这些功能的浏览器来说,很多东西都可以是“伪造的”。你知道有什么脚本可以做到这一点吗?我对 Object.create 特别感兴趣。
例如,Douglas Crockford 的 JSON 脚本 检查 JSON 函数之前是否存在创造它们。
如果有更多类似 JSON 的内容,我们可以在需要使用新函数时包含它们。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Crockford 推荐 这种
Object.create
shim:但是请不要这样做。
这种方法的问题在于 ES5
Object.create
有一个 2 个参数的签名:第一个 - 一个要继承的对象,第二个(可选) - 一个表示属性的对象(或者更确切地说,描述符)添加到新创建的对象。我们所拥有的是具有两种不同行为的不一致实现。在具有本机
Object.create
的环境中,方法知道如何处理第二个参数;在没有本机Object.create
的环境中,则不会。有什么实际意义?
好吧,如果有一些代码(例如第三方脚本)想要使用
Object.create
,那么该代码执行此操作是相当合理的:— 本质上假设如果
Object.create
存在,它必须符合规范 - 接受第二个参数并向对象添加相应的属性。但是,对于上述垫片,第二个参数被简单地忽略。甚至没有迹象表明有什么地方
出了问题。可以说,这是一种无声的失败——检测和修复起来相当痛苦。我们可以做得更好吗?
好吧,实际上不可能仅使用(标准)ES3 工具来创建完全一致的
Object.create
shim。最好的解决方案是创建自定义包装方法。然而,您可以尝试一些替代的(不是最佳的)方法:
1) 通知用户无法使用第二个参数
2) 尝试处理第二个参数:
请注意,“properties”是表示属性描述符的对象,不仅仅是属性名称/值,而且支持起来并不是很简单(有些事情甚至是不可能的,例如控制属性的可枚举性):
原始填充程序中的另一个不一致之处是它不支持不关心父对象为
null
。这将创建一个 [[Prototype]] 为
null
的对象;换句话说,对象不继承任何东西,甚至不继承Object.prototype
(ECMAScript 中的所有本机对象都继承自)。顺便说一下,这对于在 ECMAScript 中创建“正确的”哈希表很有用。
可以模拟这种行为,但只能使用非标准扩展,例如“神奇的”
__proto__
属性(因此实现不太可移植或不可靠)。此问题的解决方案类似:要么完全模拟 ES5 实现,要么通知不一致/失败。Crockford recommends this kind of
Object.create
shim: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.What we have is an inconsistent implementation with 2 different behaviors. In environments with native
Object.create
, method knows how to handle second argument; in environments without nativeObject.create
, it doesn't.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:— essentially assuming that if
Object.create
exists, it must conform to specs — accept second argument and add corresponding properties to an object.But, with the above-mentioned shim, second argument is simply ignored. There's not even an indication of something going
wrongdifferently. 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. 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
2) Try to handle second argument:
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):
The other inconsistency in the original shim is that it doesn't take care of parent object being
null
.This creates an object whose [[Prototype]] is
null
; in other words, object that doesn't inherit from anything, not evenObject.prototype
(which all native objects in ECMAScript inherit from).This is, by the way, useful to create "proper" hash tables in 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). Solution to this problem is similar: either emulate ES5 implementation fully, or notify about inconsistency/failure.es5-shim http://github.com/kriskowal/es5-shim/
这是一部分narwhal 独立 javascript 环境的一部分,但已自行突破。这是非常成熟和精确的。
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. It's pretty darn mature and precise.
es5 - JavaScript/EcmaScript 5 in 3 是在 BitBucket 上共享的集合。
Object.create
特别容易伪造,并且很流行由 Crockford 等人编写,但由 Justin Love 改进,重点关注许多内容ES5 零件。es5 - JavaScript/EcmaScript 5 in 3 is a collection shared at 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.如果您不介意学习该库并自己编写一些代码,您可以在
https://developer.mozilla.org/En/JavaScript/ECMAScript_5_support_in_Mozilla
例如,Array.filter
然后 Crockford 在 json2.js 中有 JSON.parse/stringify
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
https://developer.mozilla.org/En/JavaScript/ECMAScript_5_support_in_Mozilla
For example, the code for Array.filter
And then Crockford has JSON.parse/stringify in json2.js
https://github.com/douglascrockford/JSON-js