Object array for widgets in jQuery
I'm having a simple GUI environment with a button widget (a jQuery plugin). There's a gui object which holds all widgets like:
myGui["button1"] = button1;
myGui["button2"] = button2;
What I want to do is defini开发者_如何学Gong a widget array like:
myGui["button"][0] = button1;
myGui["button"][1] = button2;
But I'm getting an error:
myGui["button"] is undefined
What am I doing wrong here?
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];
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.
精彩评论