Yo,
My question is about javascript object.
I've read backbone.js code and i see model and object are using javascript object to define an object.
Like that
Backbone.Model.extend({
initialize: function() { ... },
author: function() { ... },
coordinates: function() { ... },
allowedToEdit: function(account) {
return true;
}
});
Why not using prototype ? Because it's redefined method for every class ? Because each object created take more space than backboneJS ?
If someone can explain it to me when and why it's interesting to use prototype ?
You seem to misunderstand. Here's the source code:
It might be confusing, but the essence here is that Backbone's
.extend
method creates new function, assigns passed object to it's prototype and returns it.As for the second question: always use prototype if you are dealing with multiple number of objects sharing the same functionalities.
Here you are extending a model, which is fine to use a JS object for. But if you want to implement an OOP class, interface or a library go for the JS prototype.
The extend method you use to create object in Backbone USE the prototype, you just don't see it.
As for the other question, I guess you got it right the way you asked your first questions :). Also, from some benchmarks I saw, using prototype is faster if you instantiate many objects. That said, if you use singleton, you may want to use static properties (extend(protoProp, staticProp)).
Related Backbone's code (extend function definition):