gpt4 book ai didi

backbone.js - 定义一个新的 Backbone 集合

转载 作者:行者123 更新时间:2023-12-04 17:51:49 24 4
gpt4 key购买 nike

我试图扩展 Backbone 集合,第一种方式是我在声明中做 new,第二种方式是我先声明,然后创建一个新实例。是做第一个错,有什么区别?

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


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

var aptlist = new AppointmentList();

最佳答案

第一个会断

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将引用扩展对象,但如果您不使用 new然后 var parent = this将引用 Backbone.Collection因为你只能调用 .apply在函数上,代码将在这里中断:
child = function(){ return parent.apply(this, arguments); };
parent将是一个对象。 Backbone.Collection是一个函数

关于backbone.js - 定义一个新的 Backbone 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14796325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com