5

私はバックボーン コレクションを拡張しようとしていました。最初の方法では宣言で new を実行し、2 番目の方法では最初に宣言してから新しいインスタンスを作成しました。最初の間違いをしている、違いは何ですか?

var AppointmentList = new Backbone.Collection.extend({
  model: Appointment
});

var AppointmentList = Backbone.Collection.extend({
  model: Appointment
});

var aptlist = new AppointmentList();
4

1 に答える 1

8

1枚目は壊れる

var Appointment = Backbone.Model.extend({
    defaults: {
        "time": "0000",
        "note": "This is an appointment"
    }
});


var AppointmentList = new Backbone.Collection.extend({

    model: Appointment

});

var aptlist = new AppointmentList();

Backbone.js には

  var extend = function(protoProps, staticProps) {

    var parent = this;
    var child;


    if (protoProps && _.has(protoProps, 'constructor')) {
      child = protoProps.constructor;
    } else {
      child = function(){ return parent.apply(this, arguments); };
    }

    _.extend(child, parent, staticProps);


    var Surrogate = function(){ this.constructor = child; };

    Surrogate.prototype = parent.prototype;

    child.prototype = new Surrogate;


    if (protoProps) _.extend(child.prototype, protoProps);


    child.__super__ = parent.prototype;

    return child;

  };

new演算子を使用して Backbone.Collection.extend をインスタンス化するvar parent = thisと、extend オブジェクトが参照されますが、使用しない場合new、thenは andvar parent = thisを参照し、関数でBackbone.Collectionのみ呼び出すことができるため.apply、コードは次のように壊れます。

child = function(){ return parent.apply(this, arguments); };

parentオブジェクトになります。Backbone.Collection関数です

于 2013-02-10T10:34:15.660 に答える