[英]Confused about Douglas Crockford's object function
I know Crockford has a famous object function for inheritance in JavaScript: 我知道Crockford在JavaScript中有一个著名的对象函数用于继承:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
But I am confused, after the line F.prototype = o
, why he doesn't he reset the F.prototype
's constructor, like this: 但是我很困惑,在
F.prototype = o
行之后,为什么他不重置F.prototype
的构造函数,如下所示:
F.prototype.constructor = F
Isn't that common practice? 这不是惯例吗?
Isn't that common practice?
这不是惯例吗?
Only when your're creating subclasses (constructor functions with prototypes inheriting from other prototypes). 仅在创建子类时(带有从其他原型继承的原型的构造函数)。
But that's not the purpose of this code, which basically Object.create
. 但这不是此代码的目的,基本上是
Object.create
。 It takes the part of creating the object which inherits from another object, nothing else. 它涉及创建从另一个对象继承的对象的过程,仅此而已。 The
F
constructor function is only intermediate and not supposed to be exposed. F
构造函数仅在中间,不应公开。
F.prototype = o;
why he doesn't he do
F.prototype.constructor = F
?为什么他不这样做
F.prototype.constructor = F
?
Because that would change o
itself. 因为那会改变
o
本身。 The aim is only to create a new object. 目的只是创建一个新对象。 Notice that it returns an instance of the intermediate constructor, not the constructor itself.
注意,它返回中间构造函数的实例 ,而不是构造函数本身。
Where would the constructor
be set ( if needed )? 将在哪里设置
constructor
( 如果需要 )? On the new object that is instantiated by object
: 在由
object
实例化的新对象上:
function inherit(chd, par) {
chd.prototype = object(par.prototype);
chd.prototype.constructor = chd;
}
function Foo() {}
function Bar() {}
inherit(Foo, Bar);
/* Because of overwriting `constructor`: */
Foo.prototype.constructor === Foo
(new Foo).constructor === Foo
/* Because of the prototype chain: */
new Foo instanceof Bar // true, because
Foo.prototype instanceof Bar // true
Since you are using an instance of F
as prototype object itself, there is no benefit in setting the constructor
property. 由于您将
F
的实例用作原型对象本身,因此设置constructor
属性没有任何好处。
For example: 例如:
function Foo() {}
function Bar() {}
Bar.prototype = object(Foo.prototype);
Bar.prototype.constructor = Bar;
So now Bar.prototype
is an instance of F
. 所以现在
Bar.prototype
是F
的实例。 We assigned the constructor
property to that instance which would shadow the constructor
property assigned to F.prototype
. 我们为该实例分配了
constructor
属性,该实例将F.prototype
分配给F.prototype
的constructor
属性。 So why bother assigning it in the first place? 那么,为什么要首先分配它呢?
Generally speaking the constructor
property has no significance in JavaScript, but it may be useful in your own code. 一般来说,
constructor
属性在JavaScript中没有意义,但是在您自己的代码中可能很有用。 Ie your code might depend on the correct value of constructor
. 即您的代码可能取决于
constructor
的正确值。 But in the object
function, F
is just a temporary constructor and object
just replicates the functionality of Object.create
, which is supported in newer browsers. 但是在
object
函数中, F
只是一个临时构造函数,而object
只是复制Object.create
的功能,新的浏览器支持该功能。 I cannot think of a use case where you want a reference to F
in your code. 我想不出一个用例,您想要在代码中引用
F
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.