在学习过程中对js的constructor的作用产生了疑难。上面是学习的材料进行梳理

function Person(area){ this.type = 'person'; this.area = area;}Person.prototype.sayArea = function(){  console.log(this.area);} var Father = function(age){ this.age = age;} Father.prototype = new Person('Beijin');console.log(Person.prototype.constructor) //function person()console.log(Father.prototype.constructor); //function person()Father.prototype.constructor = Father;     //修改console.log(Father.prototype.constructor); //function father()var one = new Father(25);

Father.prototype.constructor = Father,这里修改了的Father的constructor。咱们晓得prototype下的constructor属性返回对创立此对象的函数的援用。

一、不修改时
  Father.constructor = function Function(),Father.prototype.constructor = function Person(),这里引出
一个题外话,为什么Father.constructor !== Father.prototype.constructor。
1. _proto_是所有对象(包含函数)都有的,它才叫做对象的原型,原型链就是靠它造成的。
2. prototype只有函数(精确地说是构造函数)才有的。它跟原型链没有关系。它的作用是:构造函数new对象的时候,通知构造函数新创建的对象的原型是谁。

Father.constructor,是从Father的原型链查找属性,也就是__proto__,因为Father继承的是Function(){},而Function(){}的constructor就是它本人
所以Father.constructor = function Function();

为什么Father.prototype.constructor 是 function Person(),首先Father.prototype = new Person('Beijin');当咱们用new
运算符会产生以下步骤:

  1. var obj={}; 也就是说,初始化一个对象obj。
  2. obj.__proto__=a.prototype;
  3. a.call(obj);也就是说结构obj,也能够称之为初始化obj。

也就是说(new Person('Beijin')).__proto__ === Person.prototype //true
后面咱们说过new Person('Beijin')对象是没有prototype的,prototype只有函数才有;Father.prototype.constructor将会沿着new Person('Beijin')的
原型链向下查找constructor,new Person('Beijin')没有constructor就去它的__proto__找,因为(new Person('Beijin')).__proto__ === Person.prototype 而Person.prototype.constructor == function Person(),所以 Father.prototype.constructor == Person.prototype.constructor //function Person()
  当咱们var one = new Father(25) 时 ,one.constructor = Father.prototype.constructor,所以one.constructor指向function Person(),

二、修改时
当咱们加上Father.prototype.constructor = Father;对象one的原型链变成

不言而喻,one.constructor = Father.prototype.constructor = function Father();
三、作用 </pre>

var man;(function(){ function Father (name) { this.name = name;  }  Father.prototype.sayName= function () {    console.log(this.name);  }  man = new Father('aoyo');})()man.sayName();//aoyo console.log(Father); //Father is not defined因为Father在闭包中,当咱们想对Father类减少办法时能够通过

man.constructor.prototype.sayAge = function(age){

console.log(age);

}
man.sayAge('20'); //20

如果不进行修改,咱们的办法将会增加到Person类,而不是Father类。