我不知道我能做到這一點編程:如何以編程方式執行此操作?
var a={a:"a",b:"b"};
return a.a;
|
I want to change this programmatically, how can I do that?
不使用eval()
?
我不知道我能做到這一點編程:如何以編程方式執行此操作?
var a={a:"a",b:"b"};
return a.a;
|
I want to change this programmatically, how can I do that?
不使用eval()
?
要動態訪問對象屬性,您需要調用括號表示法窗體。像
function foo(prop) {
var a = {a:'a', b: 'b'};
return a[ prop ];
}
foo('b'); // returns 'b'
謝謝,我想我只能在數組中使用'[]'。 – 2012-02-08 03:21:00
使用a[v]
其中v
是一個變量,那些值是'a'
或'b'
。
請給我們一個測試用例。你想讓結果看起來像什麼? – Triptych 2012-02-08 03:16:19