为什么在 javascript 中列出类的实际构造函数很重要
我正在阅读 javascript Garden http://bonsaiden.github.com/JavaScript-Garden/关于 javascript 中的原型,其示例之一如下所示:
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;
请注意其中一行“确保将 Bar 列为实际构造函数”。我真的不知道这是做什么的。我尝试过创建带有或不带有最后一行的 Bar() 的新实例。但在这些实例上调用“值”或“方法”会返回完全相同的结果。所以我想知道,指定构造函数的需要是什么(我假设必须有一个)?
谢谢你!!!
I was reading on javascript garden http://bonsaiden.github.com/JavaScript-Garden/ about prototype in javascript and one of its example goes like this:
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;
Notice the line that reads Make sure to list Bar as the actual constructor. I really am lost about what this does/is. I have tried making new instances of Bar() with and without the last line. But call "value" or "method" on those instances return the exact same things. So I wonder, what's the need (I assume there must be one) of specifying the constructor?
Thank You!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个函数都有一个
prototype
属性,它在创建函数对象时被分配,它指向一个继承自Object.prototype
的新创建的对象,并且它有一个constructor
属性,它简单地指向函数本身。prototype
属性的目的是提供一种使用构造函数来实现继承的方法。当您使用new
运算符调用函数时,它将创建一个继承自该构造函数的prototype
的新对象。现在,
constructor
属性的目的是有一种方法来引用已创建对象的构造函数,例如:该属性由
Foo< 的“实例”继承。 /code>,这样你就可以知道哪个构造函数被用来创建一个对象:
如果你将一个新对象分配给函数的原型,这种关系就会丢失:
并且它也会影响函数的实例:
另一个类似的情况是当你有使用构造函数的两层或多层继承,最常见的way用来表示函数之间的继承关系,就是给第二层的
prototype
属性赋值,eg:上面的代码有几个问题,第一,它执行了父构造函数的逻辑创建继承关系,但那是另一个故事了,在上面的示例中,
constructor
属性也会受到影响,因为我们完全替换了Child.prototype
对象:如果我们替换,则替换的值分配
Child.prototype
的constructor
属性后,它将显示预期的行为:Every function has a
prototype
property, it's assigned when the function object is created, it points to a newly created object that inherits fromObject.prototype
, and it has aconstructor
property, that simply points back to the function itself.The purpose of the
prototype
property is to give a way to implement inheritance, using constructor functions. When you call a function with thenew
operator, it will create a new object that inherits from that constructor'sprototype
.Now, the purpose of the
constructor
property is to have a way to refer back to the constructor that has created an object, for example:This property is inherited by the "instances" of
Foo
, so you can know which constructor was used to create an object:If you assign a new object to the function's prototype, this relationship is lost:
And it affects also the instances of the function:
Another similar case is when you have two or more levels of inheritance using constructors, the most common way is used to denote the inheritance relationship between the functions, is to assign the
prototype
property of the second level, e.g.:The above code has several problems, first, it executes the logic of the parent constructor to create the inheritance relationship, but that's another story, in the above example the
constructor
property is also affected, since we replace completely theChild.prototype
object:If we replace replace the value of the
constructor
property ofChild.prototype
after assigning it, it will show the expected behavior:我相信这与使用 new 关键字实例化 Bar 有关。我相信使用 new 会寻找 Bar.prototype.constructor 。在该行之前,链接到 Bar.prototype.contructor 的对象是 Foo 类型,因此在没有该行的情况下实例化时,它将生成 Foo 对象而不是 Bar 对象。
I believe this has to do with instantiating Bar with the new keyword. I believe using new will look for Bar.prototype.constructor. Before that line the object linked to Bar.prototype.contructor is of type Foo so when instantiated without that line it will make a Foo object instead of a Bar object.