JS高級---原型鏈最終的指向是Object的prototype, 而Object中的__proto__是null


 原型鏈最終的指向

 

  1. 原型鏈最終的指向是Object的prototype, 而Object中的__proto__是null
  2. 如果原型指向改變了, 那么就應該在原型改變指向之后添加原型方法

 

 

    function Person() {

    }
    Person.prototype.eat = function () {
      console.log("吃東西");
    };

    var per = new Person();
    console.dir(per);
    console.dir(Person);

    //實例對象中有__proto__原型
    //構造函數中有prototype原型
    //prototype是對象
    //所以,prototype這個對象中也有__proto__,那么指向了哪里
    //實例對象中的__proto__指向的是構造函數的prototype
    //所以,prototype這個對象中__proto__指向的應該是某個構造函數的原型prototype

    //Person的prototype中的__proto__的指向
    //console.log(Person.prototype.__proto__);

    //per實例對象的__proto__------->Person.prototype的__proto__---->Object.prototype的__proto__是null

    console.log(per.__proto__ == Person.prototype); //true
    console.log(per.__proto__.__proto__ == Person.prototype.__proto__); //true 
    console.log(Person.prototype.__proto__ == Object.prototype); //true 
    console.log(Object.prototype.__proto__); //null


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM