Coming from a Java background, I expect properties in the base class of an instance of a class to be unique from other class instances that use the same base class.
In javascript, any properties stored in "base class" appear to be shared between various instances of that "base class". For instance, see here:
As you can see, BaseView.settings
is accessed via both ViewOne
and ViewTwo
. Changing a value in BaseView.settings
in one instance affects the value in the other instance.
If I move settings: {}
out of BaseView
and into ViewOne
and ViewTwo
, everything works the way I expect.
However, I don't want to clutter up ViewOne
and ViewTwo
with extra properties. So I can dynamically create this.settings
inside BaseView.setProp()
:
Are there better ways to deal with this, or is this a good solution?
Note that I'm using Backbone.JS views in this example, but I expect than any javascript prototype inheritance solution would result similarly. You can see that Backbone uses fairly typical methods of creating prototypical inheritance:
https://github.com/documentcloud/backbone/blob/0.3.3/backbone.js#L870
https://github.com/documentcloud/backbone/blob/0.3.3/backbone.js#L955