2

Please check code below. Here all variable values are static.

var o = { level_a:{}, level_b:{}, . . . .};

var levelVar = "b";

var selected_tab = 'level'+'_'+levelVar; \\level_b

var result = o.selected_tab;

Here you can see var o is object and var levelVar and selected_tab are string. Now I expect I should get value of o.level_b inside result, but its not working becuse we can not concat string to object.

Please help.

4

1 に答える 1

8

Use this notation :

result = o[selected_tab];

More generally, when you have var obj={a:'b'}, you can access the property a using both obj.a and obj['a'].

Here's a MDN reference about the use of objects and properties.

于 2012-11-12T07:59:08.280 に答える