JavaScript對象 屬性

來源:互聯網
上載者:User

標籤:

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

JavaScript對象 屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.