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

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

盡管 enumerable 設(shè)置為 true,為什么添加了 definePrope

盡管 enumerable 設(shè)置為 true,為什么添加了 definePrope

暮色呼如 2021-12-12 10:01:02
我不明白為什么不顯示屬性“salary”,盡管是可枚舉的:class People {    constructor(name, age) {        this.name = name;        this.age = age;    }}Object.defineProperty(People.prototype, 'salary', {    value: 3000,    enumerable: true,    writable: true});執(zhí)行 :> var p = new People('Jack', 46);> console.log(p)People { name: 'Jack', age: 46 }> console.log(p.propertyIsEnumerable('salary'));false> console.log(p.salary)3000為什么說 false for p.propertyIsEnumerable('salary')?但是,如果我修改該屬性,它會突然開始被枚舉:> p.salary = 45004500> console.log(p)People { name: 'Jack', age: 46, salary: 4500 }> console.log(p.propertyIsEnumerable('salary'));true這是怎么回事 ?為什么只有修改后才可枚舉?為什么從一開始就不能枚舉?
查看完整描述

1 回答

?
30秒到達(dá)戰(zhàn)場

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個贊

您使用的控制臺只記錄對象自己的屬性值,而不是來自原型的屬性值。不一定適用于所有控制臺:


class People {

    constructor(name, age) {

        this.name = name;

        this.age = age;

    }

}


Object.defineProperty(People.prototype, 'salary', {

    value: 3000,

    enumerable: true,

    writable: true

});


var p = new People('Jack', 46);

//compare with the browser console

console.log(p);

至于為什么屬性“不可枚舉”——那是因?yàn)镺bject#propertyIsEnumerable沒有遍歷原型鏈:


var a = { foo: 1 };

var b = Object.create(a);

b.bar = 2;


console.log(`a:

  a.foo: ${a.foo}

  a.hasOwnProperty("foo"): ${a.hasOwnProperty("foo")}

  a.propertyIsEnumerable("foo"): ${a.propertyIsEnumerable("foo")}`);


console.log(`b:

  b.foo: ${b.foo}

  b.hasOwnProperty("foo"): ${b.hasOwnProperty("foo")}

  b.propertyIsEnumerable("foo"): ${b.propertyIsEnumerable("foo")}

  b.bar: ${b.bar}

  b.hasOwnProperty("bar"): ${b.hasOwnProperty("bar")}

  b.propertyIsEnumerable("bar"): ${b.propertyIsEnumerable("bar")}`);


至于為什么當(dāng)你改變它時(shí)它“變得可枚舉”——事實(shí)是它不會——當(dāng)你向?qū)ο筇砑右粋€新屬性時(shí),你現(xiàn)在得到的是那個,而不是原型中的那個。原型屬性沒有改變:


var a = { foo: 1 };

var b = Object.create(a);


console.log(`b before adding a property:

  b.foo: ${b.foo}

  b.hasOwnProperty("foo"): ${b.hasOwnProperty("foo")}

  b.propertyIsEnumerable("foo"): ${b.propertyIsEnumerable("foo")}`);

  

b.foo = 2;


console.log(`b after adding a property:

  b.foo: ${b.foo}

  b.hasOwnProperty("foo"): ${b.hasOwnProperty("foo")}

  b.propertyIsEnumerable("foo"): ${b.propertyIsEnumerable("foo")}`);

  

console.log(`a after adding a property to b:

  b.foo: ${a.foo}`);


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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