为什么在 javascript 中列出类的实际构造函数很重要

发布于 2024-12-08 21:09:43 字数 686 浏览 0 评论 0原文

我正在阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

折戟 2024-12-15 21:09:43

每个函数都有一个prototype属性,它在创建函数对象时被分配,它指向一个继承自Object.prototype的新创建的对象,并且它有一个constructor 属性,它简单地指向函数本身。

prototype 属性的目的是提供一种使用构造函数来实现继承的方法。当您使用 new 运算符调用函数时,它将创建一个继承自该构造函数的 prototype 的新对象。

现在,constructor 属性的目的是有一种方法来引用已创建对象的构造函数,例如:

function Foo () {}
// default value of the property:
Foo.prototype.constructor == Foo; // true

该属性由 Foo< 的“实例”继承。 /code>,这样你就可以知道哪个构造函数被用来创建一个对象:

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

另一个类似的情况是当你有使用构造函数的两层或多层继承,最常见的way用来表示函数之间的继承关系,就是给第二层的prototype属性赋值,eg:

function Parent() {}

function Child () {}
Child.prototype = new Parent();

上面的代码有几个问题,第一,它执行了父构造函数的逻辑创建继承关系,但那是另一个故事了,在上面的示例中,constructor 属性也会受到影响,因为我们完全替换了 Child.prototype 对象:

var child = new Child();
child.constructor == Parent; // true

如果我们替换,则替换的值分配 Child.prototypeconstructor 属性后,它将显示预期的行为:

function Child() {}
Child.prototype = new Parent();
Child.prototype.constructor = Child;

var child = new Child();
child.constructor == Child; // true

Every function has a prototype property, it's assigned when the function object is created, it points to a newly created object that inherits from Object.prototype, and it has a constructor 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 the new operator, it will create a new object that inherits from that constructor's prototype.

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:

function Foo () {}
// default value of the property:
Foo.prototype.constructor == Foo; // true

This property is inherited by the "instances" of Foo, so you can know which constructor was used to create an object:

var foo = new Foo();
foo.constructor == Foo;

If you assign a new object to the function's prototype, this relationship is lost:

function Bar () {}
Bar.prototype = { inherited: 1 };

Bar.prototype.constructor == Bar;    // false
Bar.prototype.constructor == Object; // true

And it affects also the instances of the function:

var bar = new Bar();
bar.constructor == Bar;    // false
bar.constructor == Object; // true

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.:

function Parent() {}

function Child () {}
Child.prototype = new Parent();

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 the Child.prototype object:

var child = new Child();
child.constructor == Parent; // true

If we replace replace the value of the constructor property of Child.prototype after assigning it, it will show the expected behavior:

function Child() {}
Child.prototype = new Parent();
Child.prototype.constructor = Child;

var child = new Child();
child.constructor == Child; // true
美羊羊 2024-12-15 21:09:43

我相信这与使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文