You can do what you'd like to like so:
var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
alert(elements['set_1']); // beer,water,wine
See this in action at http://jsfiddle.net/x5KRD/.
All objects in JS can be accessed using dot notation (obj.method()
or obj.property
), or bracket notation (obj['method']()
or obj['property']
). Using bracket notation lets you dynamically specify method/property/key names.
For example, while clumsy, window['alert']('hi')
is equivalent to window.alert('hi')
.
Note that your code won't work as-is, anyways, because you're using object literal notation ({'beer','water','wine'}
) to contain an array (it should be in square brackets ['beer','water','wine']
instead). Object literals need to have key-value pairs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…