Extends + Implements =! memory leak?

388 views
Skip to first unread message

Roman Land

unread,
Nov 21, 2009, 6:34:27 AM11/21/09
to mootool...@googlegroups.com
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

Roman Land

unread,
Nov 22, 2009, 12:33:12 PM11/22/09
to mootool...@googlegroups.com
Hi,

Any thoughts on this??

Thanks!
Roman
--
---
"Make everything as simple as possible, but not simpler."

- Albert Einstein

Aaron Newton

unread,
Nov 22, 2009, 4:58:23 PM11/22/09
to mootool...@googlegroups.com
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:


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.

Roman Land

unread,
Nov 22, 2009, 5:22:58 PM11/22/09
to mootool...@googlegroups.com
Thanks Aaron!
Thats a great answer, exactly what I was looking for (and than some examples)!

Cheers,
Roman

Dimitar Christoff

unread,
Nov 23, 2009, 4:30:20 AM11/23/09
to mootool...@googlegroups.com
> Thanks Aaron!
> Thats a great answer, exactly what I was looking for (and than some
> examples)!
>
> Cheers,
> Roman


heheh, told you :)


--
Dimitar Christoff <chri...@gmail.com> - http://fragged.org/

Reply all
Reply to author
Forward
0 new messages