1) if you have shared objects between classes, put them in closures to avoid
global scope:

(function(){

var foo = new Hash();

this.MyClass = new Class({...}); //"this" here is the window

})();

2) the proper solution here is what you have; to declare the hash on
instantiation. If you want to be able to extend that object with subclasses,
then you can make the property an object and cast it to a hash on init:

var Foo = new Class({
  bar: {...},
  initialize: function(){
    this.h_bar = new Hash(this.bar);
  }
});
var Foo2 = new Class({
  Extends: Foo,
  bar: { ...some other stuff...},
  etc...
});


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

> haha, classic :) "this is not a bug! its a feature!" :)
>
> I am happy you find it useful, although if there's no documentation that
> says something of the sorts I wouldnt build my application around a possible
> bug..
>
> What says moo dev team?
>
>
> On Sun, Jan 31, 2010 at 10:21 PM, אריה גלזר <[email protected]>wrote:
>
>> this might actually prove to be quite useful - i can see how this might
>> let you share variables between different instances of a class without using
>> a global object. this is sometimes a very desirable functionality!
>> -----------
>> אריה גלזר
>> 052-5348-561
>> 5561
>>
>>
>>
>> On Sun, Jan 31, 2010 at 22:15, 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
>
>

Reply via email to