ES5中的Object.defineProperty? [英] Object.defineProperty in ES5?

查看:142
本文介绍了ES5中的Object.defineProperty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看到有关新Object.create的帖子,它使枚举可配置。但是,它依赖于Object.defineProperty方法。我找不到这种方法的跨浏览器实现。

I'm seeing posts about a 'new' Object.create that makes enumeration configurable. However, it relies on a Object.defineProperty method. I can't find a cross browser implementation for this method.

我们是否坚持为旧的Object.create写作?我不能写出在IE6 / 7中无效的东西。

Are we stuck writing for the old Object.create? I can't write things that won't work in IE6/7.

推荐答案

有几件你不能做的事情在ECMAScript 3环境中从ECMAScript 5 Object.create 方法模拟。

There are several things that you can't emulate from the ECMAScript 5 Object.create method on an ECMAScript 3 environment.

如您所见,属性参数会给你带来问题,因为在基于E3的实现中,没有办法来更改属性属性。

As you saw, the properties argument will give you problems since in E3-based implementations there is no way to change the property attributes.

对象.defineProperty 提到 @Raynos 的方法,适用于IE8,但部分,它只能在DOM中使用 元素。

The Object.defineProperty method as @Raynos mentioned, works on IE8, but partially, it can be used only in DOM elements.

此外访问者属性会给你带来问题,可以通过广泛支持的非标准方法模仿例如 __ defineGetter __ / __ defineSetter __ ,但再次无法更改属性属性

Also accessor properties will give you problems, they could be mimicked with widely supported non-standard methods such as __defineGetter__/__defineSetter__, but again, you can't change the property attributes.

除了属性描述符之外的另一个问题是 Object.create 方法可以接受 null 作为参数,创建一个不从任何东西继承的对象。

Another problem aside the property descriptors, is that the Object.create method can accept null as an argument, to create an object that doesn't inherits from anything.

这不能用 Crockford的 Object.create shim ,因为当 new 运算符与构造函数一起使用,该函数具有 prototype 属性,包含 null - 或者任何其他非对象值 - 默认情况下,新创建的对象将继承自 Object.prototype

This can't be emulated with the Crockford's Object.create shim, because when the new operator is used with a constructor function that has a prototype property containing null -or any other non-object value-, the newly created object will inherit from Object.prototype anyway by default.

在某些实现中-V8,Spidermonkey,Rhino等...... - 它们具有可设置的 __ proto __ 属性,可用于设置 null [[Prototype]],但再次,这是非标准的,并且肯定它永远不会在IE上工作。

In some implementations -V8, Spidermonkey, Rhino, etc...- they have a setteable __proto__ property which could be used to set a null [[Prototype]], but again, that's non-standard, and for sure it will never work on IE.

我建议,如果你想针对旧浏览器不要使用这些功能,因为没有办法让它们在这些环境中正常工作。

I would recommend, if you want to target old browsers to don't use those features, since there is no way to make them work properly on those environments.

如果你还想使用 Object.create ,不使用属性参数,你可以,但我会建议你检测一些无法模拟的东西。

If you still want to use Object.create, without using the properties argument, you could, however I would recommend you to detect the things that can't be emulated.

以下是 Crockford的对象的更安全版本。创建 shim

if (typeof Object.create != 'function') {
  (function () {
    var F = function () {};
    Object.create = function (o) {
      if (arguments.length > 1) { throw Error('Second argument not supported');}
      if (o === null) { throw Error('Cannot set a null [[Prototype]]');}
      if (typeof o != 'object') { throw TypeError('Argument must be an object');}
      F.prototype = o;
      return new F;
    };
  })();
}

无论如何,请小心使用。

Anyway, use it carefully.

这篇关于ES5中的Object.defineProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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