W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
要在對(duì)象上定義多個(gè)屬性,使用Object.defineProperties()而不是Object.defineProperty()。
此方法接受兩個(gè)參數(shù):要處理的對(duì)象和一個(gè)包含所有屬性信息的對(duì)象。
以下代碼定義了兩個(gè)屬性:
var book1 = {};
/* w w w . j av a 2 s. c o m*/
Object.defineProperties(book1, {
// data property to store data
_name : {
value : "Javascript",
enumerable : true,
configurable : true,
writable : true
},
// accessor property
name : {
get : function() {
console.log("Reading name");
return this._name;
},
set : function(value) {
console.log("Setting name to %s", value);
this._name = value;
},
enumerable : true,
configurable : true
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: