问题

虽然看过这篇博文JavaScript prototype之后对原型理解不再那么模糊了,但是依然还有很多理解不甚透彻的地方。比如,今天看到一个原型式继承的例子,又有些困惑,于是找了些帖子看看,有了一些自己的理解,贴在这里,希望理解不对的地方大家可以帮我指出来,多谢了!

先看一段代码:

function Person(){
console.log("in 'Person'");
}
function Woman(){
console.log("in 'Woman'");
} var woman=new Woman();//in 'Woman'
woman.constructor;//function Woman(){console.log("in 'woman'");} var person=new Person();//in 'Person'
person.constructor;//function Person(){console.log("in 'person'");}

以上定义了两个function,各自实例化出一个对象后,在console中查看它们的constructor,一切正常。目前两个function之间还没有任何关系,下面用prototype让它们之间挂钩。设置Woman类的prototype是Person类,这个时候再来查看constructor的情况。

function Person(){
console.log("in 'Person'");
}
function Woman(){
console.log("in 'Woman'");
}
Woman.prototype=new Person(); var woman=new Woman();//in 'Woman'
/**constructor指向的是Person函数**/
woman.constructor;//function Person(){console.log("in 'person'");} var person=new Person();//in 'Person'
person.constructor;//function Person(){console.log("in 'person'");}

可以看到woman的constructor指向了Person函数,这是为什么呢?

我的理解

我们知道,每个函数都有默认的prototype,这个prototype.constructor默认指向的就是这个函数本身。在未给Woman指定Person作为原型之前,Woman.prototype.constructor或者woman._proto_.constructor指向的就是Woman函数。但是当这样做之后:

Woman.prototype=new Person();

Woman函数的prototype被覆盖成了一个Person对象。

我们知道,constructor始终指向的是创建本身的构造函数,因此Woman.prototype.constructor自然就指向了创建Woman.prototype这个Person对象的函数,也就是Person函数。这样一来,woman对象的constructor指向就不对了,因此在网上大多关于原型继承的帖子里都会建议我们做这样的修改:

Woman.prototype.constructor=Woman;
var woman=new Woman();//in 'Woman'
woman.constructor;//function Woman(){console.log("in 'woman'");}

这样修正以后,constructor的指向就正确了。

但是,为什么要这样修正呢?不修正会有什么后果呢?

为何要做A.prototype.constructor=A这样的修正?

这是我最困惑的地方,因为我试验后发现,就算不做这样的修正,new Woman()的时候也不会有什么问题,虽然我还不理解JS是如何做到的,但是它确实找到了正确的构造函数去做实例化。最后我在stackoverflow上找到了一个回答What it the significance of the Javascript constructor property?,里面提到:

The constructor property makes absolutely no practical difference to anything internally. It's only any use if your code explicitly uses it. For example, you may decide you need each of your objects to have a reference to the actual constructor function that created it; if so, you'll need to set the constructor property explicitly when you set up inheritance by assigning an object to a constructor function's prototype property, as in your example.

所以,即使不做这样的修正也不会有什么影响,它主要防止一种情况下出错,就是你显式地去使用构造函数。比如,我并不知道woman是由哪个函数实例化出来的,但是我想clone一个,这时就可以这样:

var woman = new Woman();
...
...
...
var woman1 = woman.constructor();

为什么要做A.prototype.constructor=A这样的修正?的更多相关文章

  1. 深入理解Javascript中this, prototype, constructor

    在Javascript面向对象编程中经常需要使用到this,prototype和constructor这3个关键字. 1.首先介绍一下this的使用:this表示当前对象;如果在全局中使用this,则 ...

  2. prototype/constructor/__proto__之constructor。

    1.constructor的字面意思就是构造.它是对象的一个属性,它对应的值是该对象的“构造者” //一.构造函数实例化的对象的constructor function Cmf(n,m){ this. ...

  3. 原型模式Prototype,constructor,__proto__详解

    最近由于在找工作,又拿起<JavaScript高级程序设计>看了起来,从中也发现了自己确实还是有很多地方不懂,刚刚看到原型模式这里,今天终于搞懂了,当然,我也不知道自己的理解是否有错. 1 ...

  4. javascript Prototype constructor的理解(转)

    讲JS的构造的,这个比较清晰,但并不表示一定正确. 这几天一直在思考这个东东,感觉比以前理解更深入了. http://blog.csdn.net/chunqiuwei/article/details/ ...

  5. 彻底搞懂js __proto__ prototype constructor

    在开始之前,必须要知道的是:对象具有__proto__.constructor(函数也是对象固也具有以上)属性,而函数独有prototype 在博客园看到一张图分析到位很彻底,这里共享: 刚开始看这图 ...

  6. prototype constructor __proto__

    constructor, prototype, __proto__ 详解

  7. js中__proto__和prototype constructor 的区别和关系

    https://www.zhihu.com/question/34183746 javaScript原型.原型链的定义? prototype:每个函数都有一个prototype(显式原型),这个属性是 ...

  8. Object.prototype.constructor

    Returns a reference to the Object function that created the instance's prototype. 注意这个属性的值是函数本省的引用,而 ...

  9. 深入剖析JavaScript中的数据类型判断(typeof instanceof prototype.constructor)

    关于JavaScript中的类型判断,我想大部分JavaScripter 都很清楚 typeof 和  instanceof,却很少有人知道 constructor,以及constructor与前面二 ...

随机推荐

  1. c++/ boost 库常见错误及解决方法总结

    1. error LNK2019: 无法解析的外部符号 "class boost::system::error_category const & __cdecl boost::sys ...

  2. 【大数据】Spark内核解析

    1. Spark 内核概述 Spark内核泛指Spark的核心运行机制,包括Spark核心组件的运行机制.Spark任务调度机制.Spark内存管理机制.Spark核心功能的运行原理等,熟练掌握Spa ...

  3. King's Quest POJ - 1904(强连通分量)

    建图:王子u喜欢女孩v,则u到v连一条边.对于给出的初始完美匹配,王子u与女孩v匹配,则v到u连一条边.然后求SCC. 显然对于同一个SCC中王子数目和女孩数目是相等的,并且从某个王子出发能够到达所有 ...

  4. 使用VBA批量转换Excel格式,由.xls转换成.xlsx

    问题分析: Excel2007以前的格式是.xls,之后的格式是.xlsx.打开单独的一个Excel文档,使用“另存为”功能,可以很轻松的转换格式.但是面对几百个Excel表这样就太累了,搜索很久,也 ...

  5. MySQL的COUNT()函数理解

    MySQL的COUNT()函数理解 标签(空格分隔): MySQL5.7 COUNT()函数 探讨 写在前面的话 细心的朋友会在平时工作和学习中,可以看到MySQL的COUNT()函数有多种不同的参数 ...

  6. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

  7. 【CSS】clear清除浮动

    clear清除浮动1.作用: 规定元素的某一侧不允许存在浮动元素 2.值: 3.应用: 清除其他浮动元素对其产生的影响 <!DOCTYPE html> <html lang=&quo ...

  8. Java基础-线程安全问题汇总

    Java基础-线程安全问题汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.内存泄漏和内存溢出(out of memory)的区别 1>.什么是内存溢出 答:内存溢出指 ...

  9. 质数——6N±1法

    6N±1法求素数 任何一个自然数,总可以表示成为如下的形式之一: 6N,6N+1,6N+2,6N+3,6N+4,6N+5 (N=0,1,2,…) 显然,当N≥1时,6N,6N+2,6N+3,6N+4都 ...

  10. 《高性能MySQL》——第五章创建高性能索引

    1.创建索引基本语法格 在MySQL中,在已经存在的表上,可以通过ALTER TABLE语句直接为表上的一个或几个字段创建索引.基本语法格式如下: ALTER TABLE 表名 ADD [UNIQUE ...