JavaScript类和继承:constructor属性

JavaScript类和继承:constructor属性

2009-07-08 17:51 sanshi 博客园  我要评论(2) 字号: T |  T
一键收藏,随时查看,分享好友!

本文介绍了JavaScript里面的constructor属性。这个属性是理解JavaScript类和继承的重要基础。

AD:

constructor属性始终指向创建当前对象的构造函数。比如下面例子:

   
   
  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. // 由构造函数实例化一个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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值