zoukankan      html  css  js  c++  java
  • JavaScript对象中的constructor属性

    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 // 将上面两段代码(第6,9行)合起来,就得到下面的结论  
    12 console.log(obj.constructor.constructor === Function); // true 

    但是当constructor遇到prototype时,有趣的事情就发生了。

    我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor(prototype.constructor)默认指向这个函数。

    如下例所示:

    1.  1 function Person(name) {  
       2     this.name = name;  
       3 };  
       4 Person.prototype.getName = function() {  
       5     return this.name;  
       6 };  
       7 var p = new Person("TT");  
       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.  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("TT");  
      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. 1 Person.prototype = new Object({  //Object的实例
      2     getName: function() {  
      3         return this.name;  
      4     }  
      5 }); 

    而constructor属性始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:

    1.  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("TT");  
      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.  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;   //重新指向Person
      10 var p = new Person("TT");  
      11 console.log(p.constructor === Person);  // true  
      12 console.log(Person.prototype.constructor === Person); // true  
      13 console.log(p.constructor.prototype.constructor === Person); // true 
  • 相关阅读:
    Perl 正则匹配经验记录
    Linux——高效玩转命令行
    推荐一个SAM文件或者bam文件中flag含义解释工具
    单端测序(Single- ead)和双端测序(Pai ed-end和Mate-pai )的关系
    区别samtools faid产生的.fai文件功能和bwa index 产生的四个文件的功能
    Perl新接触的小命令
    Perl调用外部命令(其他脚本、系统命令)的方法和区别
    Linux——命令
    学习《Python金融实战》中文版PDF+英文版PDF+源代码
    学习《深度学习与计算机视觉算法原理框架应用》《大数据架构详解从数据获取到深度学习》PDF代码
  • 原文地址:https://www.cnblogs.com/ttcc/p/3748757.html
Copyright © 2011-2022 走看看