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

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

為什么突變對象的[原型]不利于性能?

為什么突變對象的[原型]不利于性能?

慕尼黑8549860 2019-07-13 14:50:20
從MDN文檔中獲取標準 setPrototypeOf功能以及非標準 __proto__財產(chǎn):改變對象的[原型],無論這是如何實現(xiàn)的,都是強烈的勸阻,因為在現(xiàn)代JavaScript實現(xiàn)中,它非常緩慢,不可避免地減緩了后續(xù)的執(zhí)行。使用Function.prototype添加屬性是這個將成員函數(shù)添加到j(luò)avascript類的方法。然后如下所示:function Foo(){}function bar(){}var foo = new Foo();// This is bad: //foo.__proto__.bar = bar; // But this is okayFoo.prototype.bar = bar;// Both cause this to be true: console.log(foo.__proto__.bar == bar); // true為什么foo.__proto__.bar = bar;壞的?如果不是壞事Foo.prototype.bar = bar;同樣糟糕?那么為什么這個警告:在現(xiàn)代JavaScript實現(xiàn)中,它非常慢,不可避免地減緩了后續(xù)的執(zhí)行。..當然Foo.prototype.bar = bar;也沒那么糟。更新也許他們所說的突變意味著重新分配。見已接受的答案。
查看完整描述

3 回答

?
MYYA

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

__proto__/setPrototypeOf與分配給對象原型不同。例如,當函數(shù)/對象的成員被分配給它時:

function Constructor(){
    if (!(this instanceof Constructor)){
        return new Constructor();
    } }Constructor.data = 1;Constructor.staticMember = function(){
    return this.data;}Constructor.prototype.instanceMember = function(){
    return this.constructor.data;}Constructor.prototype.constructor = Constructor;
    // By doing the following, you are almost doing the same as assigning to
     // __proto__, but actually not the same :Pvar newObj = Object.create(Constructor);
     // BUT newObj is now an object and not a // function like !!!Constructor!!! 
     // (typeof newObj === 'object' !== typeof Constructor === 'function'), and you
      // lost the ability to instantiate it, "new newObj" returns not a constructor, 
      // you have .prototype but can't use it. newObj = Object.create(Constructor.prototype);
       // now you have access to newObj.instanceMember // but staticMember is not available. newObj instanceof Constructor is true
       // we can use a function like the original constructor to retain // functionality, like self invoking it newObj(), accessing static 
       // members, etc, which isn't possible with Object.createvar newObj = function(){
    if (!(this instanceof newObj)){   
    
        return new newObj();
    }}; newObj.__proto__ = Constructor;newObj.prototype.__proto__ = Constructor.prototype;newObj.data = 2;(new newObj()).instanceMember(); 
    //2newObj().instanceMember(); // 2newObj.staticMember(); // 2newObj() instanceof Constructor; // is trueConstructor.staticMember();
     // 1

每個人似乎只關(guān)注原型,而忘記了函數(shù)可以分配給它的成員,并在變異后實例化。目前沒有其他方法可以不使用__proto__/setPrototypeOf..幾乎沒有人使用構(gòu)造函數(shù)而不具備從父構(gòu)造函數(shù)繼承的能力,并且Object.create不能服務(wù)。

另外,那是兩個Object.create目前,調(diào)用在V8(瀏覽器和節(jié)點)中非常緩慢,這使得__proto__更可行的選擇


查看完整回答
反對 回復(fù) 2019-07-13
?
狐的傳說

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

是的。Prototype=同樣糟糕,因此使用了“無論它如何完成”的措辭。Prototype是一個偽對象,用于在類級別擴展功能。它的動態(tài)特性減緩了腳本的執(zhí)行速度。另一方面,在實例級別添加一個函數(shù)所帶來的開銷要小得多。


查看完整回答
反對 回復(fù) 2019-07-13
  • 3 回答
  • 0 關(guān)注
  • 404 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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