Javascript Backbone.extend函数究竟是如何工作的?

Javascript Backbone.extend函数究竟是如何工作的?,javascript,oop,backbone.js,Javascript,Oop,Backbone.js,我想知道这个extend函数在Backbone.js中是如何工作的。请帮我弄清楚它到底在做什么 var extend = function(protoProps, staticProps) { var parent = this; var child; // The constructor function for the new subclass is either defined by you // (the "constructor" property i

我想知道这个
extend
函数在Backbone.js中是如何工作的。请帮我弄清楚它到底在做什么

var extend = function(protoProps, staticProps) {
    var parent = this;
    var child;

    // The constructor function for the new subclass is either defined by you
    // (the "constructor" property in your `extend` definition), or defaulted
    // by us to simply call the parent constructor.
    if (protoProps && _.has(protoProps, "constructor")) {
      child = protoProps.constructor;
    } else {
      child = function() {
        return parent.apply(this, arguments);
      };
    }

    // Add static properties to the constructor function, if supplied.
    _.extend(child, parent, staticProps);

    // Set the prototype chain to inherit from `parent`, without calling
    // `parent`'s constructor function and add the prototype properties.
    child.prototype = _.create(parent.prototype, protoProps);
    child.prototype.constructor = child;

    // Set a convenience property in case the parent's prototype is needed
    // later.
    child.__super__ = parent.prototype;

    return child;
  };

这就是为什么要将父变量添加到子变量?

extend
采用两个参数
protoProps
staticProps
protoProps
是将分配给类prototype的属性,以便在创建对象实例时,对象将该属性作为其prototype链的一部分
staticProps
是从类创建的对象(使用
new
)无法使用的道具,但可以从类本身访问,例如,通过调用
CatClass.defaultMeow

var extend = function(protoProps, staticProps) {
    var parent = this;
    var child;
在下面的讨论中,
父类
是我们将称之为基类的类,我们希望将其原型扩展到
子类
,在这里我们将其称为扩展类

    // The constructor function for the new subclass is either defined by you
    // (the "constructor" property in your `extend` definition), or defaulted
    // by us to simply call the parent constructor.
    if (protoProps && _.has(protoProps, "constructor")) {
      child = protoProps.constructor;
    // Set the prototype chain to inherit from `parent`, without calling
    // `parent`'s constructor function and add the prototype properties.
    child.prototype = _.create(parent.prototype, protoProps);
如果
protoProps
是一个函数,或者具有
constructor
属性(这是在类上调用
new
时调用的属性(作为方法)

如果没有,扩展类将使用父类的
构造函数
(当您调用
new
时,它将调用父类的
构造函数
方法)

.extend(target,src1,…,srcN)
下划线JS方法将源对象的属性浅拷贝到目标对象。这里复制了所有的父(静态)属性,所有的属性都传递到
staticProp
对象(如果提供)到新的扩展类

    // The constructor function for the new subclass is either defined by you
    // (the "constructor" property in your `extend` definition), or defaulted
    // by us to simply call the parent constructor.
    if (protoProps && _.has(protoProps, "constructor")) {
      child = protoProps.constructor;
    // Set the prototype chain to inherit from `parent`, without calling
    // `parent`'s constructor function and add the prototype properties.
    child.prototype = _.create(parent.prototype, protoProps);
这可能是Backbone.extend例程最重要的功能:这是扩展类“继承”基类原型链的地方。例如,若
AnimalClass.prototype.walk
AnimalClass
的原型链中的一个方法,
\uCreate(parent.prototype,protoProps)
将使用这个新类原型链中的
walk
方法以及传入的所有
原型创建一个新类。本质上,这就是“扩展原型链”,它被分配给扩展类,作为它的原型

 child.prototype.constructor = child;
这一行一开始令人困惑,因为我们在上面的条件语句中看到扩展类已经被分配了一个构造函数。的确如此,但在上一条语句中,当我们创建(…)
时,我们用基类构造函数重写了扩展类构造函数!现在我们正在重新分配它

    // Set a convenience property in case the parent's prototype is needed
    // later.
    child.__super__ = parent.prototype;
正如注释所说,扩展类可以访问***static属性*
\uuuuuuuuuuuuuuuuuuuuuuu
中的基类。它是一个方便的属性,可以从扩展类对象本身访问。在我们前面的示例中,如果一个
CatClass
是从
AnimalClass
扩展而来的,那么以下是正确的:
CatClass.\uuu super\uu===AnimalClass

    return child;
  };