When you create a new class MooTools dereferences any objects using the
Object.reset method (not documented). Go look at the source of Class.js.
You'll see that this method iterates over all the properties, dereferencing
all the objects and arrays to prevent this pollution. However, it switches
on the type of property, dereferencing arrays and object. Hash, however,
returns $type "hash" (*$type($H({})) == "hash"*), so it doesn't dereference.
That's why instantiating the hash from a property on instantiation will
work.
On Sun, Jan 31, 2010 at 1:05 PM, Roman Land <[email protected]> wrote:
> errr not initialization class - initialization method ..
>
>
> On Sun, Jan 31, 2010 at 11:04 PM, Roman Land <[email protected]> wrote:
>
>> @Aaron, good tip, will see if I can use that also.
>>
>> @Eneko, that is my understanding also, although knowing JS I am not sure
>> how it will behave further down the road when they overwrite the same value,
>> they might split into two different objects with overlaps and complement
>> each other.. (my JS experience with this is limited.. please someone correct
>> me if I am wrong..)
>> Also, I dont want to overload the initialization class, this looks messy
>> and hard to understand what are the instance variables are..
>>
>> Might I suggest this "feature" be documented somehow? :)
>>
>> I will open a light house ticket to see if I can pass this ..
>>
>> On Sun, Jan 31, 2010 at 10:54 PM, Eneko Alonso <[email protected]>wrote:
>>
>>> 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
>>>>
>>>>
>>>
>>
>>
>> --
>> ---
>> "Make everything as simple as possible, but not simpler."
>>
>> - Albert Einstein
>>
>>
>
>
> --
> ---
> "Make everything as simple as possible, but not simpler."
>
> - Albert Einstein
>
>