JavaScript 描述符:enumerable/configurable/writable 默认情况下不正确?

JavaScript Descriptors: enumerable/configurable/writable are not true by default?

我正在 new ES5 features 上阅读 John Resig 的一篇博客,其中一篇是 Object 描述符。约翰在他的博客早些时候写道:

The three attributes (writable, enumerable, and configurable) are all optional and all default to true. Thus, the only property that you’ll need to provide will be, either, value or get and set.

稍后他提供了一个例子:

var obj = {};

Object.defineProperty( obj, "propertyname", {
    value: true,
    writable: false,
    enumerable: true,
    configurable: true
});

(function() {
    var name = "John";

    Object.defineProperty( obj, "name", {
        get: function(){ return name; },
        set: function(value){ name = value; }
    });
})();

但是,当检查 name 的 属性 描述符时,它不可配置或可枚举,因此我无法在 for 循环中访问它。

console.log(Object.getOwnPropertyDescriptor(obj, 'name'))
> Object {enumerable: false, configurable: false}

不应该 name 既可枚举又可配置吗?我在这里错过了什么?

描述符属性的默认值为 false 或未定义。

博客有错误。除非在描述符中明确定义,否则 enumerable/configurable/writable 将是 false,而 value 将是 undefined。访问器描述符属性也将默认为 undefined

这与 MDN 并发 notes:

Both data and accessor descriptors are objects. They share the following required keys:

configurable

true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object. Defaults to false.

enumerable

true if and only if this property shows up during enumeration of the properties on the corresponding object. Defaults to false.

A data descriptor also has the following optional keys:

value

The value associated with the property. Can be any valid JavaScript value (number, object, function, etc). Defaults to undefined.

writable

true if and only if the value associated with the property may be changed with an assignment operator. Defaults to false.

An accessor descriptor also has the following optional keys:

get

A function which serves as a getter for the property, or undefined if there is no getter. The function return will be used as the value of property. Defaults to undefined.

set

A function which serves as a setter for the property, or undefined if there is no setter. The function will receive as only argument the new value being assigned to the property. Defaults to undefined.

the spec 开始,数据属性默认为 non-configurable、non-enumerable 和 non-writable。

If the initial values of a property's attributes are not explicitly specified by this specification, the default value defined in Table 4 is used.

Table 4: Default Attribute Values

┌──────────────────┬─────────────────┐
│  Attribute Name  │  Default Value  │
├──────────────────┼─────────────────┤
│ [[Value]]        │ undefined       │
│ [[Get]]          │ undefined       │
│ [[Set]]          │ undefined       │
│ [[Writable]]     │ false           │
│ [[Enumerable]]   │ false           │
│ [[Configurable]] │ false           │
└──────────────────┴─────────────────┘

但是,当您通过赋值创建数据 属性 时,CreateDataProperty 将其定义为可配置、可枚举和可写。

Let newDesc be the PropertyDescriptor{[[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}.