JavaScript对象 属性

除了包含名字和值外,属性还包含了一些他们可写、可枚举、可配置的特性。JavaScript中包含两种属性:数据属性和访问器属性。

数据属性:

configurable:表示能否通过delete删除属性从而重新定义属性,能否修改属性的特性,或者能把属性改成访问器属性。

enumerable:表示能否通过for-in循环返回属性。

writable:表示能否修改属性的值。

value:包含这个属性的数据值。

要修改属性默认的特性,使用Object.defineProperty()。

        var person = {};
        Object.defineProperty(person, "name", {
            configurable: false,//一旦把属性定义为不可配置,就不能再修改为可配置。
            writable: false,
            value: "Nicholas"
        });
        delete person.name;
        alert(person.name); //Nicholas
        person.name = "Michael";
        alert(person.name); //Nicholas

访问器属性:

configurable:表示能否通过delete删除属性从而重新定义属性,能否修改属性的特性,或者能把属性改成访问器属性。

enumerable:表示能否通过for-in循环返回属性。

get:在读取属性时调用的函数。

value:在写入属性时调用的函数。

       var book = {
            _year: 2004,
            edition: 1
        };
          
        Object.defineProperty(book, "year", {
            get: function(){
                return this._year;
            },
            set: function(newValue){
            
                if (newValue > 2004) {
                    this._year = newValue;
                    this.edition += newValue - 2004;
                
                }
            }
        });
        
        book.year = 2005;
        alert(book.edition);   //2
Note: this example only works in browsers that have implemented the ECMAScript 5

定义多个属性 Object.defineProperties()

         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;
                    }                  
                }            
            }        
        });
           
        book.year = 2005;
        alert(book.edition);   //2
Note: this example only works in browsers that have implemented the ECMAScript 5

读取属性的特性

        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"
Note: this example only works in browsers that have implemented the ECMAScript 5

 

原文地址:https://www.cnblogs.com/YuanSong/p/3899287.html