Reputation: 957
I'm learning backbone and I have a very simple question, Code below works fine:
var segments = Backbone.Model.extend({
url: url;
});
var segments = new Segments();
but if I put new while extending then it doesn't. for eg:
var segments = new Backbone.Model.extend({
url: url;
});
Can someone explain why?
Upvotes: 0
Views: 285
Reputation: 3455
You are trying to instantiate extend method,which is used to copy properties to a constructor for later instantiation.
Here is that extend method:
var extend = function(protoProps, staticProps) {
1520 var parent = this;
1521 var child;
1522
1523 // The constructor function for the new subclass is either defined by you
1524 // (the "constructor" property in your `extend` definition), or defaulted
1525 // by us to simply call the parent's constructor.
1526 if (protoProps && _.has(protoProps, 'constructor')) {
1527 child = protoProps.constructor;
1528 } else {
1529 child = function(){ return parent.apply(this, arguments); };
1530 }
1531
1532 // Add static properties to the constructor function, if supplied.
1533 _.extend(child, parent, staticProps);
1534
1535 // Set the prototype chain to inherit from `parent`, without calling
1536 // `parent`'s constructor function.
1537 var Surrogate = function(){ this.constructor = child; };
1538 Surrogate.prototype = parent.prototype;
1539 child.prototype = new Surrogate;
1540
1541 // Add prototype properties (instance properties) to the subclass,
1542 // if supplied.
1543 if (protoProps) _.extend(child.prototype, protoProps);
1544
1545 // Set a convenience property in case the parent's prototype is needed
1546 // later.
1547 child.__super__ = parent.prototype;
1548
1549 return child;
1550 };
With Backbone.Model.extend({}) you are just calling a function, with provided arguments.
Upvotes: 0
Reputation: 2232
This is not really backbone related but more javascript in general.
In your example:
var segments = new Backbone.Model.extend({
url: url
});
"new" operator has the highest precedence so it's evaluated first (before Backbone.Model.extend() is executed). So you are really trying to instantiate an object from the extend() function, not its return value.
It should work if you change it to :
var segments = new (Backbone.Model.extend({
url: url
}));
In this case. extend() function gets called first and returns an object (which is a model definition in backbone).
But it's not a good practice. You are defining a model(in the parentheses) and throwing it away(not keeping the definition in a variable) at the same time.
You can find more about javascript operator precedence here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
Upvotes: 0
Reputation: 3727
The keyword new is used to instantiate a model not to define it or extend it. so
var Segments = Backbone.Model.extend({ /// Capitalize your model definition
url: url // no semicolon here
}); ///here you are defining a regular Backbone Model
var OtherSegments = Segments.extend({
url: url
}); ///here you are extending your model
var segments = new Segments(); //this is how you instanciate a BB model
//and use lower case to differentiate the definition
//for the instanciation set to variable.
var otherSegments = new OtherSegments();
var mySegments = new Segments({ url : url}); // you can pass values at the time of
//instanciatation
Upvotes: 1