Defining a New Family Collection

I tried to expand the base collections, the first way I did in the declaration, and in the second I declared first, and then created a new instance. Does the first wrong, what's the difference?

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

and

 var AppointmentList = Backbone.Collection.extend({ model: Appointment }); var aptlist = new AppointmentList(); 
+4
source share
1 answer

The first will break

 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(); 

In Backbone.js we have

  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; }; 

If you create an instance of Backbone.Collection.extend using the new operator, then var parent = this will refer to the extension object, but if you do not use new , then var parent = this will refer to Backbone.Collection and since you can only call functions .apply for functions, here the code will be broken:

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

parent will be the object. Backbone.Collection is a function

+7
source

All Articles