I have a base view V:
V = Backbone.View.extend({
initialize: function() {
console.log(this.options.z);
console.log(this.options.q);
}
});
extended by VV, which sets the value of a property (q: 234):
VV = V.extend({q:234});
V is further specialized to VV:
new VV({z: 123})
The problem is that the base type has no access to q, how can I do this?
I’m trying to understand this inheritance system, in this example
q and z are like virtual/abstract values that are to be defined in
sub classes.
When you say this:
you’re creating
VVas “subclass” ofVthat will have an extraqproperty. The properties defined by the “class” have nothing to do with the automatic options handling fromView#initialize.The documentation might be better if it said “creating a new instance of the View”.
Then, when you
new VV({z: 123}), that{z: 123}is passed to the view’sinitializemethod in its singleoptionsparameter (pre-1.1.0 Backbones automatically setthis.optionsto the constructor’s options) theqwill be inthis.qso yourinitializeshould look more like this:Also, if you did this:
you’d be replacing the default
qthat instances ofVVget andthis.qwould be'pancakes'insideinitialize.Demo: http://jsfiddle.net/ambiguous/eqBV2/
Using
View#extendis like subclassing (or more accurately it creates a new prototypical instance) whereasnewcreates new objects (or copies of the prototypical instance). Of course, the class/instance language doesn’t fit the reality of JavaScript so we have to be careful not to take the terminology too seriously.