I have the follow sample code:
type = 'Foo';
test = {
type: {
'fooVal': 'bar'
}
}
alert(test.type.fooVal); // Bar
alert(test.Foo.fooVal); // TypeError: teste.Foo is undefined
How I can get the second alert to work?
I’ve tried:
test = {
'"' + type + '"': {
'fooVal': 'bar'
}
}
But doesn’t work.
Use bracket notation to do the assignment instead:
You can’t use variables as keys when assigning via object notation.