JavaScript類和繼承:constructor屬性

來源:互聯網
上載者:User

constructor屬性始終指向建立當前對象的建構函式。比如下面例子:

 
  1. // 等價於 var foo = new Array(1, 56, 34, 12);  
  2. var arr = [1, 56, 34, 12];  
  3. console.log(arr.constructor === Array); // true  
  4. // 等價於 var foo = new Function();  
  5. var Foo = function() { };  
  6. console.log(Foo.constructor === Function); // true  
  7. // 由建構函式執行個體化一個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時,有趣的事情就發生了。

我們知道每個函數都有一個預設的屬性prototype,而這個prototype的constructor預設指向這個函數。如下例所示:

 
  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 

當時當我們重新定義函數的prototype時(注意:和上例的區別,這裡不是修改而是覆蓋),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屬性始終指向建立自身的建構函式,所以此時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 
轉自:http://developer.51cto.com/art/200907/134913.htm
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.