Quote:
Originally Posted by CyanFloor
Therefore is it true to say that putting properties in the prototype object that are object itselves is wrong?
|
i don't think it is wrong per sè, but most of the time, we manage the state of an object using it's properties. Generally, you will see .prototype used more for holding methods to act upon the objects. At times .prototype is used for inheritance.
Quote:
Originally Posted by CyanFloor
Why it doesn't happen if i set scalar properties?
|
primitives like numbers, booleans, and strings are passed by value, whereas objects are passed by reference.
wikipedia explains the details well, but just know that when setting two variables to the same object, both instances share that one object.
setting two vars to a string will make each var its own copy.
objects (including arrays) = shared instances
primitives = isolated instances
Quote:
Originally Posted by CyanFloor
There ins't a way to "unlink" the object properties from the prototype?
|
store them in the object themselves, not in the prototoype. jkd showed an example of how to do this.
remember there are two copies of Klass-built objects: klass1+klass2, but there is only one Klass.prototype (an object) that all instances of objects made from the Klass constructor will share. if you ask for klass2.a, and a is not a member of klass2, Klass.prototype will be looked at to see it it has an "a" property. if it does, Klass.prototype.a is returned, if not, undefined is returned.