本文介绍了when是实际使用的对象的`.constructor`属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用这样的代码继承自另一个对象:

I've been using code like this for inheriting from another object:

// define base object constructor
function SuperType(){
    // constructor code
}

// define base object methods on the prototype
SuperType.prototype.foo = function() {};

// ---------------------------------------------------------------------------

// define object that wants to inherit from the SuperType object
function SubType() {
    // call base object constructor with all arguments that might have been passed
    SuperType.apply(this, arguments); 
    // other constructor code
}

// set prototype for this object to point to base object
// so we inherit any items set on the base object's prototype
SubType.prototype = new SuperType();
// reset constructor to point to this object not to SuperType
SubType.prototype.constructor = SubType;

// define any methods of this object by adding them to the prototype
SubType.prototype.myMethod = function() {};

我的问题是为什么你必须设置 SubType.constructor = SubType ?何时是 .constructor 属性实际使用?如果你创建一个 SubType 对象:

My question is why do you have to set the SubType.constructor = SubType? When is the .constructor property actually used? If you create a SubType object like this:

var s = new SubType();

这将调用 SubType()构造函数,不管 SubType.prototype.constructor 实际设置为什么,所以我试图理解 .constructor 属性是否实际使用?

That's going to call the SubType() constructor regardless of what SubType.prototype.constructor is actually set to, so I'm trying to understand when the .constructor property is ever actually used?

正如你可以在与 .constructor 注释掉,正确的构造函数仍然被调用。因此,在 new 运算符的正常构造中,不会使用 .constructor 属性。我想知道什么时候使用它?

As you can see in this jsFiddle demo with the assignment to .constructor commented out, the proper constructor is still called. So, it appears that the .constructor property is not used in normal construction with the new operator. I'm wondering when it is used?

推荐答案

根据,

换句话说,只是一个指向Object的实际,本地构造函数的指针 - 通过新的Object()调用的那个。因此,你可以覆盖它而不改变任何关于新的Object()如何工作。

In other words, it's just a pointer to the Object's actual, native constructor-- The one that is called via new Object(). Thus, you can override it without changing anything about how new Object() works.

那么为什么它存在?因为构造函数在JavaScript中是有效的类,它允许你使用类实例做某些事情,而不需要它们的类名。要借用,构造函数属性允许:

So why does it exist? Because constructor functions are effectively classes in JavaScript, it lets you do certain things with class instances without needing to their class name. To borrow from Axel Rauschmayer's blog, the constructor property allows:


  • 比较类之间的比较,ieaconstructor === b.constructor

  • 从现有实例创建一个新实例,即new a.constructor()

  • 调用另一个(超级)类的构造函数

没有对类名的任何硬编码引用。再次,它不是真的意味着被覆盖。为了回应上述评论,我从来没有见过有人在野外重写它。

All without any hardcoded references to class names. Again, it's not really meant to be overridden. To echo the above comments, I've never really seen someone override it in the wild.

这篇关于when是实际使用的对象的`.constructor`属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 02:39