简体   繁体   English

对道格拉斯·克罗克福德的对象函数感到困惑

[英]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.prototypeF的实例。 We assigned the constructor property to that instance which would shadow the constructor property assigned to F.prototype . 我们为该实例分配了constructor属性,该实例将F.prototype分配给F.prototypeconstructor属性。 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.

相关问题 Douglas Crockford的书中关于Function.prototype的“方法” - 'method' on Function.prototype in Douglas Crockford's book 当我用箭头功能重写时,为什么不能将IIFE与Douglas Crockford的风格一起使用? - Why can't an IIFE be used with Douglas Crockford's style when I rewrite it with an arrow function? 道格拉斯克罗克福德的构造模式 - Constructor pattern by Douglas Crockford 有人可以解释道格拉斯·克罗克福德的超级方法吗? - Can someone please explain Douglas Crockford's uber method? Douglas Crockford 的“Javascript:好的部分”第 5.5 章 - Douglas Crockford's "Javascript: The Good Parts" Chapter 5.5 使用Douglas Crockford的函数继承在Javascript中调用基本方法 - Call base method in Javascript using Douglas Crockford's functional inheritance 应用 Douglas Crockford 的构图模式时分享 state - Sharing state when applying Douglas Crockford's composition pattern 为什么Douglas Crockford的monad演示代码需要monad原型? - Why is a monad prototype required for Douglas Crockford's monad demo code? 解释这个 javascript 函数是如何工作的(在 Douglas Crockford 'How Javascript Works' 中找到) - explain how this javascript function works (found in Douglas Crockford 'How Javascript Works') Douglas Crockford在JavaScript中的Class Free OOP - Douglas Crockford on Class Free OOP in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM