欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript中構(gòu)造函數(shù)與原型鏈之間的關(guān)系詳解

 更新時間:2019年02月25日 08:36:52   投稿:laozhang  
在本篇文章里小編給大家分享了關(guān)于JavaScript中構(gòu)造函數(shù)與原型鏈之間的關(guān)系相關(guān)知識點,需要的朋友們學(xué)習(xí)下。

在Javascript中不存在class的概念,它的class概念是通過構(gòu)造函數(shù)(constructor)與原型鏈(prototype)來實現(xiàn)。

1.構(gòu)造函數(shù)(constructor):創(chuàng)建對象時的初始化對象,總是與new 關(guān)鍵是一同出現(xiàn)。

構(gòu)造函數(shù)存在以下特點:

  • 1、構(gòu)造函數(shù)內(nèi)的this 指向當(dāng)前實例對象。
  • 2、使用new 關(guān)鍵字實例化當(dāng)前對象。
  • 3、構(gòu)造函數(shù)首字母大寫,區(qū)分普通函數(shù)。
  • 4、實例對象都可以繼承構(gòu)造函數(shù)中的屬性和方法。但是,同一個對象實例之間,無法共享屬性。

2.原型(prototype):是一個對象,實現(xiàn)對象的屬性繼承。javascript 中的對象通過 proto 來指向原型對象,可以通過Object.__proto__ 來訪問

3.構(gòu)造函數(shù)與與原型的聯(lián)系:

 <script>
  function Demo(){
       
   }
   var demo = new Demo()
   var data= demo.prototype = function(){
 
   }
   
   console.log(demo.__proto__)
   console.log(data.constructor )
   console.log(data.prototype.__proto__)
   console.log(demo.constructor.prototype)
   console.log(demo.constructor)
  輸出:
  {constructor: ƒ}constructor: ƒ Demo()__proto__: Object
  ƒ Function() { [native code] }
  {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
  {constructor: ƒ}
  ƒ Demo(){  }
</script>

從以上輸出結(jié)果可以看出:

構(gòu)造函數(shù)的 __proto__ 指向原型對象;
原型的constructor 指向構(gòu)造函數(shù)Function;
原型的 prototype.__proto__ 等于 Object.__proto__;
實例 的 constructor.prototype 指向原型;
實例的constructor 指向構(gòu)造函數(shù)

引用圖例:

![1460000018155881][1]

相關(guān)文章

最新評論