I’ve seen a Javascript project where a prototype property is defined like this:
myFunc.prototype.a = new myObject()
I’m wondering what happens when I call new myFunc() to the a property:
Does it return the result of new myObject() or everytime I call myFunc.a it calls new myObject()?
And on different myFunc instances the a property is the same one as it happens for normal prototype properties or every instance’s a is different myObject() instance?
See this http://backbonejs.org/docs/todos.html: every TodoList instance will share the same localStorage, so the same Backbone.LocalStorage() instance?
Hopefully, this will help you out:
The “prefer composition to inheritance” mantra goes quadruple for JavaScript.
You can quite happily override a particular object on a person:
Just be careful when modifying properties of the prototype object (the object which instances refer to).
Reason being that you changed a property of the shared, prototype object (static, in other languages), rather than replacing the WHOLE prototype object (referencing a new object, instead of the static object) like in Jim’s case.
But the
X.prototype.y = new Z();can be seen like this, simply: