Thanks Aaron! Thats a great answer, exactly what I was looking for (and than some examples)!
Cheers, Roman On Sun, Nov 22, 2009 at 11:58 PM, Aaron Newton <[email protected]> wrote: > if you want to understand how all this works you need to read class.js, > which is pretty thick stuff. > > As you can see with a little more work, classes that are implemented into > others create instances and copy their properties into the target class. > Look at this: > > http://mooshell.net/ESgYF/2 > > You can see that I implement a new method into FirstClass, but all the > classes that have FirstClass implemented are not updated with this new > method. However, instances of FirstClass (I create an instance called > "first") ARE altered when I update the class. > > This is because an instance of a class has a pointer to the original class > object as its prototype. Altering that prototype alters all instances of > that class. > > But when you implement a class into another, it creates an instance of the > implemented class and assigns the properties of that instance to the > prototype of the new one. The resulting copied properties + the properties > defined when you create the target class become the prototype of that class. > Like so: > > var A = new Class(objA); > var instanceOfA = new A(); > instanceOfA.prototype == objA; > objA.foo = "bar"; > instanceOfA.foo == "bar"; > instanceOfA.foo = "baz"; //no longer looks for prototype's value > > instanceOfA doesn't have a "foo" property, so the interpreter looks at its > prototype (objA) to find it. Altering the prototype alters the instances. If > you assign a value to the instance it no longer looks at the prototype. > > Now, when we implement a class into another one, this is what happens: > > var objA = { > "oof": "rab" > }; > var A = new Class(objA); > objB = { > Implements: A, > foo: "bar" > }; > var B = new Class(objB); > > var instanceOfA = new A(); > > delete.objB.Implements; > > for(prop in instanceOfA) objB[prop] = instanceOfA[prop]; > > //objB is now altered to include the properties of objA > > Changes to objA no longer affect instance of B, but changes to objB do. > Further, objB, the prototype of the B class, now has all the values in objA. > They have the same references, so they do not consume additional memory, but > if you alter A through assignment: > > objA.oof = "zab"; > > you won't alter objB, which still has a reference to "rab". > > However, every time you create an instance of any of these class, you > aren't creating copies; you are just creating new objects whose prototypes > are the objects you started with. > > > > On Sun, Nov 22, 2009 at 9:33 AM, Roman Land <[email protected]> wrote: > >> Hi, >> >> Any thoughts on this?? >> >> Thanks! >> Roman >> >> >> On Sat, Nov 21, 2009 at 1:34 PM, Roman Land <[email protected]> wrote: >> >>> Dear Moos ;) >>> >>> First, to illustrate my question http://mooshell.net/baYDF/1 >>> >>> What I am trying to figure out is - >>> By extending a class that by itself implements few other classes, causes >>> the extended class to copy (be the definition of "Implements") implemented >>> classes every time it is extended (and later on initialized?...), or is it >>> doing this statically for the extended classes as well? >>> >>> How do you figure things like that anyway? (if something is copied vs its >>> referenced statically) >>> >>> Cheers, >>> Roman >>> >>> >> >> >> -- >> --- >> "Make everything as simple as possible, but not simpler." >> >> - Albert Einstein >> >> > -- --- "Make everything as simple as possible, but not simpler." - Albert Einstein
