偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

JavaScript類和繼承:constructor屬性

開(kāi)發(fā) 前端
本文介紹了JavaScript里面的constructor屬性。這個(gè)屬性是理解JavaScript類和繼承的重要基礎(chǔ)。

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

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

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

我們知道每個(gè)函數(shù)都有一個(gè)默認(rèn)的屬性prototype,而這個(gè)prototype的constructor默認(rèn)指向這個(gè)函數(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. // 將上兩行代碼合并就得到如下結(jié)果  
  12. console.log(p.constructor.prototype.constructor === Person); // true 

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

  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 

為什么呢?

原來(lái)是因?yàn)楦采wPerson.prototype時(shí),等價(jià)于進(jìn)行如下代碼操作:

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

而constructor屬性始終指向創(chuàng)建自身的構(gòu)造函數(shù),所以此時(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 

怎么修正這種問(wèn)題呢?方法也很簡(jiǎn)單,重新覆蓋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種實(shí)現(xiàn)方式總結(jié)
  4. JavaScript異步調(diào)用框架的鏈?zhǔn)綄?shí)現(xiàn)
  5. JavaScript異步調(diào)用框架的jQuery風(fēng)格鏈?zhǔn)秸{(diào)用
責(zé)任編輯:yangsai 來(lái)源: 博客園
相關(guān)推薦

2009-07-08 17:42:26

this屬性

2009-07-08 17:48:18

prototype屬性

2011-05-25 16:23:35

Javascript類繼承

2009-01-04 09:08:30

面向?qū)ο?/a>繼承接口

2011-08-31 14:48:33

JavaScript

2012-02-14 09:45:02

JavaScript

2017-07-21 09:40:35

Python類、繼承和多態(tài)

2012-11-08 10:40:47

JavaScript原型鏈

2009-09-09 11:28:40

Scala類

2011-07-08 10:25:55

JavaScript

2011-07-07 13:39:14

Cocoa 框架

2011-08-24 13:56:27

JavaScript

2023-10-18 16:30:50

2021-12-04 11:17:32

Javascript繼承編程

2011-03-10 14:19:56

JavaScript

2010-09-09 09:16:27

JavaScriptdiv

2021-12-09 10:51:47

Go繼承

2023-09-27 23:28:28

Python編程

2020-04-28 10:05:33

JavaScript繼承前端

2016-12-27 09:10:29

JavaScript原型鏈繼承
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)