每个函数都有一个prototype
属性,它是在创建函数对象时分配的,它指向一个新创建的继承自 的对象Object.prototype
,并且它有一个constructor
属性,它简单地指向函数本身。
该prototype
属性的目的是提供一种使用构造函数来实现继承的方法。当您使用运算符调用函数时new
,它将创建一个继承自该构造函数的新对象prototype
。
现在,该constructor
属性的目的是有一种方法可以引用已创建对象的构造函数,例如:
function Foo () {}
// default value of the property:
Foo.prototype.constructor == Foo; // true
此属性由 的“实例”继承Foo
,因此您可以知道使用哪个构造函数创建对象:
var foo = new Foo();
foo.constructor == Foo;
如果您将新对象分配给函数的原型,则此关系将丢失:
function Bar () {}
Bar.prototype = { inherited: 1 };
Bar.prototype.constructor == Bar; // false
Bar.prototype.constructor == Object; // true
它也会影响函数的实例:
var bar = new Bar();
bar.constructor == Bar; // false
bar.constructor == Object; // true
另一个类似的情况是,当你使用构造函数有两级或多级继承时,最常用的方式是表示函数之间的继承关系,是分配prototype
第二级的属性,例如:
function Parent() {}
function Child () {}
Child.prototype = new Parent();
上面的代码有几个问题,首先,它执行了父构造函数的逻辑来创建继承关系,但那是另一回事了,在上面的例子中,constructor
属性也受到了影响,因为我们完全替换了Child.prototype
对象:
var child = new Child();
child.constructor == Parent; // true
如果我们在分配后替换替换constructor
属性的值Child.prototype
,它将显示预期的行为:
function Child() {}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
var child = new Child();
child.constructor == Child; // true