第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaScript中Object.defineProperty()的奇怪行為

JavaScript中Object.defineProperty()的奇怪行為

守著星空守著你 2021-05-03 16:18:23
我在玩下面的javascript代碼。了解后Object.defineProperty(),我正面臨一個奇怪的問題。當(dāng)我嘗試在瀏覽器或VS代碼中執(zhí)行以下代碼時,輸出與預(yù)期不符,而如果我嘗試對代碼進行調(diào)試,則輸出正確當(dāng)我調(diào)試代碼并評估配置文件時,我可以name & age在對象中看到該屬性,但是在輸出時,它僅顯示該name屬性//Code Snippet let profile = {  name: 'Barry Allen',}// I added a new property in the profile object.Object.defineProperty(profile, 'age', {  value: 23,  writable: true})console.log(profile)console.log(profile.age)現(xiàn)在這里的預(yù)期輸出應(yīng)該是{name: "Barry Allen", age: 23}23但我得到的輸出為。請注意,我能夠訪問age之后定義的屬性。我不確定為什么console.log()會這樣。{name: "Barry Allen"}23 
查看完整描述

3 回答

?
猛跑小豬

TA貢獻1858條經(jīng)驗 獲得超8個贊

您應(yīng)該設(shè)置enumerabletrue。在Object.definePropertyfalse默認(rèn)情況下。根據(jù)MDN的說法。

枚舉

true當(dāng)且僅當(dāng)該屬性顯示了相應(yīng)的對象的屬性的枚舉期間。

默認(rèn)為false。

不可枚舉意味著該屬性將不會在控制臺中顯示Object.keys()for..in循環(huán)顯示

let profile = {

    name: 'Barry Allen',

}


// I added a new property in the profile object.


Object.defineProperty(profile , 'age', {

    value: 23,

    writable: true,

    enumerable: true

})

console.log(profile)

console.log(profile.age)

prototype內(nèi)置類的對象的所有屬性和方法都是不可枚舉的。這就是您可以從實例中調(diào)用它們但它們在迭代時不出現(xiàn)的原因。

獲取所有屬性(包括不可枚舉)Object.getOwnPropertyNames() 。

let profile = {

    name: 'Barry Allen',

}


// I added a new property in the profile object.


Object.defineProperty(profile , 'age', {

    value: 23,

    writable: true,

    enumerable: false

})

for(let key in profile) console.log(key) //only name will be displayed.


console.log(Object.getOwnPropertyNames(profile)) //You will se age too


查看完整回答
反對 回復(fù) 2021-05-06
?
慕絲7291255

TA貢獻1859條經(jīng)驗 獲得超6個贊

默認(rèn)情況下,您使用定義的屬性defineProperty是不可枚舉的-這意味著當(dāng)您對其進行迭代時,這些屬性將不會顯示Object.keys(這是代碼段控制臺所做的事情)。(類似地,由于length無法枚舉數(shù)組的屬性,因此無法顯示。)

參見MDN

數(shù)不清的

當(dāng)且僅當(dāng)在枚舉相應(yīng)對象的屬性時顯示此屬性時,才返回true。

默認(rèn)為false。

使其可枚舉:

//Code Snippet 

let profile = {

  name: 'Barry Allen',

}


// I added a new property in the profile object.

Object.defineProperty(profile, 'age', {

  value: 23,

  writable: true,

  enumerable: true

})


console.log(profile)

console.log(profile.age)

您可以在記錄的圖像中看到該屬性的原因是,Chrome的控制臺也將向您顯示不可枚舉的屬性-但不可枚舉的屬性將略顯灰色

http://img1.sycdn.imooc.com//6093a9ad000160a603850243.jpg

看看age灰色是多少,而name不是灰色-這表明它name是可枚舉的,而age不是。


查看完整回答
反對 回復(fù) 2021-05-06
?
絕地?zé)o雙

TA貢獻1946條經(jīng)驗 獲得超4個贊

每當(dāng)使用對象的“ .defineProperty”方法時。您最好定義描述符的所有屬性。因為如果您不定義其他屬性描述符,則它將假定所有屬性描述符的默認(rèn)值為false。因此,您的console.log檢查所有可枚舉的true屬性,并將它們記錄下來。


//Code Snippet 

let profile = {

  name: 'Barry Allen',

}


// I added a new property in the profile object.

Object.defineProperty(profile, 'age', {

  value: 23,

  writable: true,

  enumerable : true,

  configurable : true

})


console.log(profile)

console.log(profile.age)


查看完整回答
反對 回復(fù) 2021-05-06
  • 3 回答
  • 0 關(guān)注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號