我正在阅读 javascript garden http://bonsaiden.github.com/JavaScript-Garden/关于 javascript 中的原型(prototype)及其示例之一是这样的:
function Foo() {
this.value = 42;
}
Foo.prototype = {
method: function() {}
};
function Bar() {}
// Set Bar's prototype to a new instance of Foo
Bar.prototype = new Foo();
Bar.prototype.foo = 'Hello World';
// Make sure to list Bar as the actual constructor <-------------------
Bar.prototype.constructor = Bar;
注意读作 Make sure to list Bar as the actual constructor 的行。我真的不知道这是做什么/是什么。我尝试制作带有和不带有最后一行的 Bar() 的新实例。但是在这些实例上调用“值”或“方法”会返回完全相同的东西。所以我想知道,指定构造函数有什么必要(我假设必须有一个)?
谢谢!!!
最佳答案
每个函数都有一个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;
如果您将一个新对象分配给函数的原型(prototype),这种关系就会丢失:
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
如果我们在分配后替换 Child.prototype
的 constructor
属性的值,它将显示预期的行为:
function Child() {}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
var child = new Child();
child.constructor == Child; // true
关于javascript - 为什么在 javascript 中列出类的实际构造函数很重要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719119/