views:

10

answers:

2

hi,

i'm having some simple gui environment with a button widget (=jquery plugin) there's a gui object which holds all widgets like:

myGui["button1"] = button1;
myGui["button2"] = button2;

what i want to do is defining a widget array like:

myGui["button"][0] = button1;
myGui["button"][1] = button2;

but i'm getting an error: myGui["button"] is undefined anyone can tell me what i'm doing wrong here?

thx

+1  A: 

You need to first do:

myGui["button"] = [];

This creates an array, after that you can use it as one.

On an unrelated note, you can use a bit nicer (in my opinion, anyway) syntax:

myGui.button = [];
myGui.button[0] = button1;
myGui.button[1] = button2;

Also if you want to simply always append at the end of array, you don't need to specify [n] yourself, but can use .push():

myGui.button = [];
myGui.button.push(button1);
myGui.button.push(button2);

It does the same thing ultimately.

reko_t
+2  A: 

Make sure you set up the array first:

myGui['button'] = [];

then

myGui['button'][0] = button1;

should work. Or, you could do this:

myGui['button'] = [button1, button2];
Pointy