How can I use the value of the variable a
as a key to lookup a property? I want to be able to say: b["whatever"]
and have this return 20:
var a = "whatever";
var b = {a : 20}; // Want this to assign b.whatever
alert(b["whatever"]); // so that this shows 20, not `undefined`
I am asking if it's possible during the creation of b
, to have it contain "whatever":20
instead of a:20
where "whatever" is itself in a variable. Maybe an eval
could be used?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…