admin管理员组文章数量:1425717
I was reading one of John Resig's blogs on new ES5 features, one being Object
descriptors. Earlier in his blog, John writes:
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.
Later on he provides an example:
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; }
});
})();
However, when checking the property descriptor for name
, it is not configurable or enumerable and consequently I am unable to access it in a for loop.
console.log(Object.getOwnPropertyDescriptor(obj, 'name'))
> Object {enumerable: false, configurable: false}
Shouldn't name
be both enumerable and configurable? What am I missing here?
I was reading one of John Resig's blogs on new ES5 features, one being Object
descriptors. Earlier in his blog, John writes:
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.
Later on he provides an example:
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; }
});
})();
However, when checking the property descriptor for name
, it is not configurable or enumerable and consequently I am unable to access it in a for loop.
console.log(Object.getOwnPropertyDescriptor(obj, 'name'))
> Object {enumerable: false, configurable: false}
Shouldn't name
be both enumerable and configurable? What am I missing here?
-
1
the default is actually
false
when usingObject.defineProperty
, it'strue
for "normal" props created by literals/assignment. i think this simplifies coding the usual defaults for such dynamic props. look atvar obj = {}; Object.defineProperty( obj, "propertyname", { value: true});Object.getOwnPropertyDescriptor(obj, 'propertyname');
for a sanity check. – dandavis Commented Jan 17, 2017 at 21:22 -
1
The blog is mistaken. The default is
false
when usingObject.defineProperty
. – castletheperson Commented Jan 17, 2017 at 21:27 - @dandavis Are you saying: if you assign a property's value in its descriptor, then other properties will automatically be set to true? – Govind Rai Commented Jan 18, 2017 at 0:28
- Just tested the above--other properties still false. Regardless, you're both correct: Unless explicitly defined in the descriptor, enumerable/configurable/writable will be false and value will be undefined. – Govind Rai Commented Jan 18, 2017 at 0:50
- That reference you cite must be at least five years old, and may have been written at a point in time when the spec was still fluid. – user663031 Commented Jan 18, 2017 at 1:29
2 Answers
Reset to default 5From the spec, data properties are non-configurable, non-enumerable and non-writable by default.
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 │ └──────────────────┴─────────────────┘
However, when you create a data property via an assignment, then CreateDataProperty defines it as configurable, enumerable and writable.
Let newDesc be the PropertyDescriptor{[[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}.
The default values for descriptor properties is false or undefined.
The blog has an error. Unless explicitly defined in the descriptor, enumerable
/configurable
/writable
will be false
and value
will be undefined
. Accessor descriptor properties will default to undefined
as well.
This is concurrent with the 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.
本文标签: JavaScript Descriptors enumerableconfigurablewritable are not true by defaultStack Overflow
版权声明:本文标题:JavaScript Descriptors: enumerableconfigurablewritable are not true by default? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745450347a2658854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论