>From my understanding, without getting in depth of how Mootools works, here
is what happens:

When the js parser executes the line "MyClass = new Class.." it creates the
native object that is passed to the Class function. In order to create this
native object, it has to create an instance of a Hash, which gets assigned
to the myHash property.

So far, we have a hash object created in memory and the result object
returned by Class. This resulting object has a pointer to the Hash object in
memory: the myHash property.

When you create new instances of this object, you are just cloning that
pointer, which keeps pointing to the same Hash object in memory. Again,
these two lines do not create a new instance of a Hash, but just clone the
pointer to the original instance.
a = new MyClass();
b = new MyClass();

If I'm not mistaken, this would happen with any object instanced in the
declaration of a class.

In the other hand, when you put something in the initialize method (class
constructor), it does not get executed until the class (MyClass) is
instantiated.

That's why, in general, class properties should be only static values and
everything that requires instantiating new objects should be done in the
constructor.

Please, let me know if I'm wrong.




On Sun, Jan 31, 2010 at 12:15 PM, Roman Land <[email protected]> wrote:

> Hi,
>
> I found out (the "bug" way ;) ) that if I write a class like so:
>
> MyClass = new Class({
>
>     myHash : new Hash()
>
> })
>
> Later I will use this class
>
> a = new MyClass();
>
> b = new MyClass();
>
> I would seem that the hash is actually being shared :P
> My work around is to init the hash in the "initialize" method.
>
> Any thought about this? I dont like the aesthetics of this
> implementation...
>
> Cheers
> Roman
>
> --
> ---
> "Make everything as simple as possible, but not simpler."
>
> - Albert Einstein
>
>

Reply via email to