我有一个JavaScript对象这样的JavaScript如何使用一个变量引用一个对象
var obj={
a:{x: "someValue", y:"anotherValue"},
b:{x: "bValue", y:"anotherbValue"}
};
,我试图像这样引用它
function(some_value){
alert("some_value is " + some_value + " with type " + typeof some_value);
// prints some_value is a with type string
var t;
t=obj[some_value]["x"]; // doesn't work
some_value="a";
t=obj[some_value]["x"]; // this does work
t=obj["a"]["x"]; // and so does this
}
我真的想了解什么是去这里。理想情况下,我想引用我 对象与传递给函数的值。 感谢
对不起,有一个错字 - 实际代码中最后一个对象没有两个双引号 – sferret 2012-04-29 01:43:50
您可以编辑您的问题。 http://stackoverflow.com/posts/10369028/edit – 2012-04-29 01:44:12
如果'some_value'确实' “一个”','然后T = OBJ [SOME_VALUE] [ “×”];'将工作。 – 2012-04-29 01:46:21