日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JavaScript類和繼承:constructor屬性

constructor屬性始終指向創(chuàng)建當前對象的構造函數(shù)。比如下面例子:

專業(yè)成都網(wǎng)站建設公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設,五站合一網(wǎng)站設計制作,服務好的網(wǎng)站設計公司,做網(wǎng)站、網(wǎng)站制作負責任的成都網(wǎng)站制作公司!

 
 
 
  1. // 等價于 var foo = new Array(1, 56, 34, 12);  
  2. var arr = [1563412];  
  3. console.log(arr.constructor === Array); // true  
  4. // 等價于 var foo = new Function();  
  5. var Foo = function() { };  
  6. console.log(Foo.constructor === Function); // true  
  7. // 由構造函數(shù)實例化一個obj對象  
  8. var obj = new Foo();  
  9. console.log(obj.constructor === Foo); // true  
  10.  
  11. // 將上面兩段代碼合起來,就得到下面的結論  
  12. console.log(obj.constructor.constructor === Function); // true 

但是當constructor遇到prototype時,有趣的事情就發(fā)生了。

我們知道每個函數(shù)都有一個默認的屬性prototype,而這個prototype的constructor默認指向這個函數(shù)。如下例所示:

 
 
 
  1. function Person(name) {  
  2.     this.name = name;  
  3. };  
  4. Person.prototype.getName = function() {  
  5.     return this.name;  
  6. };  
  7. var p = new Person("ZhangSan");  
  8.  
  9. console.log(p.constructor === Person);  // true  
  10. console.log(Person.prototype.constructor === Person); // true  
  11. // 將上兩行代碼合并就得到如下結果  
  12. console.log(p.constructor.prototype.constructor === Person); // true 

當時當我們重新定義函數(shù)的prototype時(注意:和上例的區(qū)別,這里不是修改而是覆蓋),constructor屬性的行為就有點奇怪了,如下示例:

 
 
 
  1. function Person(name) {  
  2.     this.name = name;  
  3. };  
  4. Person.prototype = {  
  5.     getName: function() {  
  6.         return this.name;  
  7.     }  
  8. };  
  9. var p = new Person("ZhangSan");  
  10. console.log(p.constructor === Person);  // false  
  11. console.log(Person.prototype.constructor === Person); // false  
  12. console.log(p.constructor.prototype.constructor === Person); // false 

為什么呢?

原來是因為覆蓋Person.prototype時,等價于進行如下代碼操作:

 
 
 
  1. Person.prototype = new Object({  
  2.     getName: function() {  
  3.         return this.name;  
  4.     }  
  5. }); 

而constructor屬性始終指向創(chuàng)建自身的構造函數(shù),所以此時Person.prototype.constructor === Object,即是:

 
 
 
  1. function Person(name) {  
  2.     this.name = name;  
  3. };  
  4. Person.prototype = {  
  5.     getName: function() {  
  6.         return this.name;  
  7.     }  
  8. };  
  9. var p = new Person("ZhangSan");  
  10. console.log(p.constructor === Object);  // true  
  11. console.log(Person.prototype.constructor === Object); // true  
  12. console.log(p.constructor.prototype.constructor === Object); // true 

怎么修正這種問題呢?方法也很簡單,重新覆蓋Person.prototype.constructor即可:

 
 
 
  1. function Person(name) {  
  2.     this.name = name;  
  3. };  
  4. Person.prototype = new Object({  
  5.     getName: function() {  
  6.         return this.name;  
  7.     }  
  8. });  
  9. Person.prototype.constructor = Person;  
  10. var p = new Person("ZhangSan");  
  11. console.log(p.constructor === Person);  // true  
  12. console.log(Person.prototype.constructor === Person); // true  
  13. console.log(p.constructor.prototype.constructor === Person); // true 

【編輯推薦】

  1. JavaScript類和繼承:prototype屬性
  2. JavaScript類和繼承:this屬性
  3. ExtJS Grid Tooltip的3+1種實現(xiàn)方式總結
  4. JavaScript異步調(diào)用框架的鏈式實現(xiàn)
  5. JavaScript異步調(diào)用框架的jQuery風格鏈式調(diào)用

文章題目:JavaScript類和繼承:constructor屬性
文章源于:http://www.5511xx.com/article/cdjjeoi.html