javascript - Object data and accessor configurable and enumerable attributes not true by default?
大家讲道理
大家讲道理 2017-06-26 10:58:19
0
3
635
    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页)
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
黄舟

The default configurable properties and enumerable properties of the object are true

This is correct, but this refers to when you do not use defineProperty or defineProperties. When you use them, those two properties default to false.

Enumerable properties are those properties whose internal [[Enumerable]] flag is set to true, which is the default for properties created via simple assignment or via a property initializer (properties defined via Object.defineProperty and such default [[Enumerable ]] to false).

defineProperties

漂亮男人

This API manual has instructions, and the default is false.

ringa_lee

The red envelope book says that for properties defined directly on the object, their default value is true; something like this,
`

var book = {
    a: 1   //默认configurable就是ture
}
//以下不同
Object.defineProperty(book,{
    b:{
        value: 5 //为配置configurable,则默认false
    },
    c:{
        value:4,
        configurable: true //有配置,为true
    }
})

`
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'.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template