Javascript 构造函数属性的意义是什么? [英] What it the significance of the Javascript constructor property?

查看:32
本文介绍了Javascript 构造函数属性的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图绕开 Javascript 对 OO 的看法……并且,像许多其他人一样,对 constructor 属性感到困惑.特别是 constructor 属性的重要性,因为我似乎无法让它产生任何影响.例如:

function Foo(age) {this.age = 年龄;}功能栏(){Foo.call(this, 42);this.name = "baz";}Bar.prototype = Object.create(Foo.prototype);var b = 新酒吧;警报(b.构造函数);//"福".没关系,因为我们继承了 `Foo` 的原型.警报(b.name);//巴兹".显示 Bar() 被称为构造函数.警报(b.年龄);//"42",继承自 `Foo`.

在上面的例子中,对象 b 似乎有正确的构造函数叫做 (Bar) –它从 Foo 继承了 age 属性.那么为什么很多人建议这是一个必要的步骤:

Bar.prototype.constructor = Bar;

很明显,在构造b时调用了正确的Bar构造函数,那么这个prototype属性有什么影响呢?我很想知道正确"设置构造函数属性实际上有什么实际区别 - 因为我看不出它对创建对象后实际调用哪个构造函数有任何影响.

解决方案

2020 年 9 月更新

下面的答案来自 ECMAScript 3 时代,第一句话不再正确,因为从 ECMAScript 6 开始,constructor 属性在几个地方使用.但是,我认为总体要点仍然适用.感谢 T. J. Crowder 在评论中指出这一点,请阅读他的回答以更全面地了解当前情况.

原答案

constructor 属性对内部的任何东西都没有实际影响.只有当您的代码明确使用它时,它才有任何用处.例如,您可能决定需要每个对象都引用创建它的实际构造函数;如果是这样,当您通过将对象分配给构造函数的 prototype 属性来设置继承时,您需要显式设置 constructor 属性,如您的示例所示.>

Trying to bend by head around Javascript's take on OO...and, like many others, running into confusion about the constructor property. In particular, the significance of the constructor property, as I can't seem to make it have any effect. E.g.:

function Foo(age) {
    this.age = age;
}

function Bar() {
    Foo.call(this, 42);
    this.name = "baz"; 
}

Bar.prototype = Object.create(Foo.prototype); 
var b = new Bar;    

alert(b.constructor); // "Foo". That's OK because we inherit `Foo`'s prototype.
alert(b.name);        // "baz". Shows that Bar() was called as constructor.
alert(b.age);         // "42", inherited from `Foo`.

In the above example, the object b seems to have had the right constructor called (Bar) – and it inherits the age property from Foo. So why do many people suggest this as a necessary step:

Bar.prototype.constructor = Bar;

Clearly, the right Bar constructor was called when constructing b, so what impact does this prototype property have? I am curious to know what practical difference it actually makes to have the constructor property set 'correctly'—as I can't see it having any affect on which constructor is actually called after an object is created.

解决方案

September 2020 Update

The answer below is from the days of ECMAScript 3 and the first sentence is no longer true because since ECMAScript 6, the constructor property is used in a few places. However, I think the overall gist still applies. Thanks to T. J. Crowder for pointing that out in the comments, and please read his answer for a fuller picture of the current situation.

Original answer

The constructor property makes absolutely no practical difference to anything internally. It's only any use if your code explicitly uses it. For example, you may decide you need each of your objects to have a reference to the actual constructor function that created it; if so, you'll need to set the constructor property explicitly when you set up inheritance by assigning an object to a constructor function's prototype property, as in your example.

这篇关于Javascript 构造函数属性的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆