js原型對(duì)象與原型鏈

首先我們要先了解幾個(gè)概念,js萬(wàn)物皆對(duì)象,所有類型(對(duì)象,函數(shù),數(shù)組...)都有一個(gè)屬性 proto,只有函數(shù)才有屬性prototype。
下面我們先來(lái)看一下構(gòu)造函數(shù)屬性constructor

function Person(name, age) {
      this.name = name;
      this.age = age;
      this.sayName = function() { alert(this.name) };
  }
  var person1 = new Person("BurC", 21);
  var person2 = new Person("VinX", 22);
  console.log(person1.constructor == Person); //true
  console.log(person2.constructor == Person); //true

person1 和 person2 都是 Person 的實(shí)例。這兩個(gè)實(shí)例都有一個(gè) constructor (構(gòu)造函數(shù))屬性,該屬性指向 Person。 所以說(shuō)這個(gè)construct屬性指向的是他的構(gòu)造函數(shù)。

找原型鏈?zhǔn)窃趯傩訽_proto__ 屬性上去找,而不是在屬性prototype上面去找。

function Person(){}; 
var burc = new Person();
console.log(burc.constructor  === Person); //true
console.log(burc.__proto__ === Person .prototype); //true
console.log(burc.__proto__ === burc.constructor.prototype); //true
console.log(burc.__proto__); //Person()  {}
console.log(burc.__proto__.__proto__); //Object()  {}
console.log(burc.__proto__.__proto__.__proto__); //null

burc 的 __proto__ 屬性指向 burc的構(gòu)造函數(shù)的原型對(duì)象
柏成畫了兩張圖幫助大家理解


QQ圖片20190826164233.png
QQ圖片20190826203405.png

參考鏈接
https://www.cnblogs.com/shuiyi/p/5305435.html

最后編輯于
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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