views:

26

answers:

1
game_state = function(){
    this.players = function() {
        this.x = 0;
    };
}

game_state.players['test'] = 1;

Why does this fail but

game_state['test'] = 1;

does not?

I'm trying this in node.js in case it wasn't clear. Thanks for any help.

+1  A: 

game_state is a (constructor) function. Instances of the function have a players property, but the function itself does not. I think you may want:

game_state = new (function()
             { 
               this.players = new (function()
                              {
                                this.x = 0;
                              })();
             })();
game_state.players['test'] = 1;

EDIT: The same applies to the inner function. Also, in both cases, you can probably use object literals.

Matthew Flaschen
ahh... so obvious now ;) Thank you
alex brown