JavaScript | 資料屬性與訪問器屬性

來源:互聯網
上載者:User

標籤:查看屬性   bottom   ie8   fine   個數   return   str   property   ***   

屬性類型

資料屬性 - 包含一個資料值的位置,可以讀取和寫入值

[writable]

是否能修改屬性的值

true

[enumerable]

是否通過for in 迴圈返回屬性(是否可以被枚舉)

true

[configurable]

是否能通過delete刪除,能否修改屬性的特性,能否修改訪問器屬性

true

[value]

包含這個屬性的資料值,讀取屬性值的時候從這個位置讀取。

undefined

訪問器屬性

[enumerable]

是否通過for in 迴圈返回屬性(是否可以被枚舉)

true

[configurable]

是否能通過delete刪除,能否修改屬性的特性,能否修改訪問器屬性

true

[get]

讀取屬性時調用的函數

undefined

[set]

寫入屬性時調用的函數

undefined

屬性操作

  • 定義屬性:Object.defineProperty()
  • 查看屬性:Object.getOwnPropertyDescriptor()

    <<script.js>>

"use strict";// *****************************************************************// 操作資料屬性var person = {    name: ‘hugh‘,    age: 29,    sayName: function() { console.log(1); }}// 修改屬性預設特性:Object.defineProperty()Object.defineProperty(person, "name", {    writable: true,    value: ‘dong‘,    configurable: false,    enumerable: false});console.log(person);// *****************************************************************// 操作訪問器屬性var book = {    _year: 2004, // _作為標記只能通過對象訪問的屬性    edition: 0};Object.defineProperty(book, "year", {    // 訪問器屬性year包含setter和getter函數    get: function() {        return this._year;    },    set: function(newValue) {        this._year = newValue;        this.edition = newValue - 2004;    }})book.year = 2008;console.log(book);console.log(book.edition);// 舊方法,ie8部分不支援defineProperty()// strict 模式不可用// book._defineGetter_("year",function(){//     return this._year;// });// book._defineSetter_("year",function(newValue){//     this._year = newValue;//     this.edition = newValue - 2014;// });// *****************************************************************// 定義多個屬性var book2 = {};Object.defineProperties(book2, {    // 資料屬性    _year: {        value: 2004,        writable: false,        enumerable: false,        configurable: true    },    edition: {        value: 0,        writable: false,        enumerable: false,        configurable: true    },    // 訪問器屬性    year: {        get: function() {            return this._year;        },        set: function(newValue) {            this._year = newValue;            this.edition = newValue - 3000;        }    }});console.log(book2);// *****************************************************************// 查看屬性的屬性console.log(Object.getOwnPropertyDescriptor(book2,‘_year‘));console.log(Object.getOwnPropertyDescriptor(book2,‘edition‘));console.log(Object.getOwnPropertyDescriptor(book2,‘year‘));

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.