var book = {};
Object.defineProperties(book, {
_year: {
value: 2004
},
edition: {
value: 1
},
year: {
get: function(){
return this._year;
},
set: function(newValue){
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
}
}
});
var descriptor = Object.getOwnPropertyDescriptor(book, "_year");
alert(descriptor.value); //2004
alert(descriptor.configurable); //false
alert(typeof descriptor.get); //"undefined"
var descriptor = Object.getOwnPropertyDescriptor(book, "year");
alert(descriptor.value); //undefined
alert(descriptor.enumerable); //false
alert(typeof descriptor.get); //"function"
以下这两行看不懂
alert(descriptor.configurable); //false
alert(descriptor.enumerable); //false
对象默认的可配置属性和可枚举属性是true吧
示例代码未设置这两个属性,默认的应该为true吧
PS:红宝书6.1.3(24印143页)
This is correct, but this refers to when you do not use
defineProperty
ordefineProperties
. When you use them, those two properties default tofalse
.defineProperties
This API manual has instructions, and the default is false.
The red envelope book says that for properties defined directly on the object, their default value is true; something like this,
`
`
I pulled out my red envelope book. Yes, it means attributes directly defined on the object. The configurable feature defaults to true. No, you can output the configurable values of 'a', 'b', and 'c'.