如何理解CoffeeScript生成的JavaScript代码';s`extends`关键字

如何理解CoffeeScript生成的JavaScript代码';s`extends`关键字,javascript,coffeescript,prototypal-inheritance,Javascript,Coffeescript,Prototypal Inheritance,这是由CoffeeScript的extends关键字生成的JavaScript代码。原型链是如何设置的 var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }

这是由CoffeeScript的
extends
关键字生成的JavaScript代码。原型链是如何设置的

var __hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { 
    for (var key in parent) { 
        if (__hasProp.call(parent, key)) child[key] = parent[key]; 
    } 
    function ctor() { this.constructor = child; } 
    ctor.prototype = parent.prototype; 
    child.prototype = new ctor; 
    child.__super__ = parent.prototype; 
    return child; 
};

请参阅(我自己的博客帖子)

哪一部分给您带来了麻烦?不理解这一行:
ctor.prototype=parent.prototype我的博客文章详细说明了这一点<代码>构造函数
称为代理构造函数。它是一个单独的构造函数,您可以将父对象的原型复制到它。它设置原型链
child.prototype=new-ctor
,而不必仅为设置继承而调用父级构造函数。更为人所知(但有问题)的设置继承的方法是执行
child.prototype=newparent
。再次,我的博客文章详细介绍了我读到你的博客文章时遇到的问题。解释得很透彻。关键是理解“代理构造函数”。谢谢我知道这不是OP感兴趣的部分,但是你知道为什么这段代码会这样使用
\uuu hasProp
?我不明白这与说if(parent.hasOwnProperty(key))相比有什么帮助。…即使在父级上覆盖了一个属性
parent.hasOwnProperty
,它仍然有效。有时写为
{}.hasOwnProperty.call(parent,key)
var __hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) {
    // Copy "static" attributes from the parent constructor to the child constructor
    for (var key in parent) { 
        if (__hasProp.call(parent, key)) child[key] = parent[key]; 
    } 
    // This is the surrogate constructor, used so you don't need
    // to instantiate an instance of the parent just to setup the prototype chain
    // the statement in the surrogate constructor properly attaches
    // the constructor property to object
    function ctor() { this.constructor = child; }
    // Attach the parent's prototype to the surrogate constructor
    ctor.prototype = parent.prototype; 
    // This is setting up the chain, attaching an instance of a constructor whose
    // prototype is set to the parent to the prototype property of the child
    // In naive implementations, this would be child.prototype = new parent();
    child.prototype = new ctor; 
    // Allows access to the parent from user code, and used by the `super` keyword
    child.__super__ = parent.prototype; 
    return child; 
};