5

Let's take the following code:

var obj = {};
var x = Symbol();
Object.defineProperties(obj, {
  [x]: {
    value: true,
    writable: true
  },
  "property2": {
    value: "Hello",
    writable: false
  }
  // etc. etc.
});
console.log(obj[x])

Is this valid?

With the native Object.defineproperties code we get in the console.log true.

With the polyfill of zone.js

which is of the form of :

  Object.defineProperties = function (obj, props) {
    Object.keys(props).forEach(function (prop) {
      Object.defineProperty(obj, prop, props[prop]);
    });
    return obj;
  };

we get for the same code of console.log undefined.

This is because of the Object.keys function. I googled around and did not find in any place if this should be allowed or not.

4

1 回答 1

2

我四处搜索,并没有在任何地方找到是否应该允许这样做。

您可以随时查看规范,在本例中为§ 19.1.2.3 Object.defineProperties(O, Properties)

它使用OwnPropertyKeysinternal 方法,它确实列出了对象的所有字符串和符号键。

这是因为Object.keys功能

正确的。应该是Object.getOwnPropertyNames(props).concat(Object.getOwnPropertySymbols(props))。你可能想用 zone.js 提交一个错误。然而,我确实想知道为什么在使用 ES6 符号时需要为 ES5Object.defineProperties函数添加 polyfill?

于 2016-03-20T12:01:40.053 回答