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

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

有什么區(qū)別?

有什么區(qū)別?

夢(mèng)里花落0921 2022-09-16 22:03:03
代碼一:obj.hasOwnProperty(prop);代碼二:const hasOwnProperty = Object.prototype; hasOwnProperty.call(obj, prop);我總是使用第一種編碼風(fēng)格,但我在一些JS書籍或github項(xiàng)目中多次看到第二種編碼風(fēng)格。我想知道這只是一種習(xí)慣,或者真的是編寫js代碼的更好方法。謝謝。
查看完整描述

1 回答

?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果不被篡改,則差異相對(duì)較小。但是,如果您依賴于包含原型函數(shù),那么各種各樣的東西都會(huì)妨礙您期望它工作(甚至可能是偶然的)。這就是為什么您經(jīng)常會(huì)看到庫(kù)直接調(diào)用原型方法,而不是依賴于它們對(duì)單個(gè)對(duì)象的完整性。objobj


請(qǐng)考慮以下情況:


const prop = 'test';


obj = {test: 'example'};

console.log({

 'version': 'no tampering!',

 'obj.hasOwnProperty': obj.hasOwnProperty(prop),

 'Object.prototype.hasOwnProperty': Object.prototype.hasOwnProperty.call(obj, prop)

});


// someone does something you don't expect with obj

obj.hasOwnProperty = () => false;

// NOTE: This at least is a function, there is nothing to stop me from setting it to something that would BREAK the function call...

// E.g. obj.hasOwnProperty = 42;


console.log({

 'version': 'some tampering!',

 'obj.hasOwnProperty': obj.hasOwnProperty(prop),

 'Object.prototype.hasOwnProperty': Object.prototype.hasOwnProperty.call(obj, prop)

});

或者,如果 對(duì)象不繼承原型呢?objObject


const obj =  Object.create(null);


console.log(obj.hasOwnProperty);


查看完整回答
反對(duì) 回復(fù) 2022-09-16
  • 1 回答
  • 0 關(guān)注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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