原型鏈整理

image.png

總結(jié):

  1. new的本質(zhì)
  2. 指向總結(jié)
    • Object.__ proto __ === Function.prototype // true
    • Object.prototype.__ proto __ === null // true
    • Function.prototype.__ proto __ === Object.prototype // true
    • 所有內(nèi)置對(duì)象都是Function對(duì)象的實(shí)例,F(xiàn)unction的__ proto __ 指向的是Function.prototype
  • 屬性的查找是先找自身屬性,如果沒有找到,沿著__ proto __屬性向上查找,直到null。
  1. 實(shí)現(xiàn)一個(gè)new/instanceof

    function new () {
    let obj = {}
    let con = [].shift.call(arguments)
    obj.__ proto __ = con.prototype
    let reuslt = con.apply(obj, arguments)
    return result instanceof Object ? result : obj
    }

    function instanceof (left, right) {
    while(left) {
    if (left.__ proto __ === right.prototype) {
    return
    }
    }
    return false
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容