可能重复:
How can I access local scope dynamically in javascript?的Javascript:其名称可以访问在封闭的局部变量或变量
大家好。
我们都知道你可以使用[]语法通过它的名称访问JavaScript对象的属性。 OB [ 'nameOfProperty']。
你可以做同样的局部变量吗?另一个回答here建议答案是使用window ['nameOfVar']。但是,这只适用于海报,因为他是在窗口范围内定义变量的。
我认为这一定是可能的,因为Firefox的Firebug(我相信是用javascript编写的)可以显示局部变量和闭包变量。有没有我不知道的一些隐藏的语言功能?
具体而言,这里就是我想要做的:
var i = 4;
console.log(window['i']); // this works..
function Func(){
var j = 99;
// try to output the value of j from its name as a string
console.log(window['j']); // unsurprisingly, this doesn't work
}
Func();
请参阅http://stackoverflow.com/questions/598878/how-can-i-access-local-scope-dynamically-in-javascript – 2010-02-25 19:16:29
你也可以看看http://stackoverflow.com/questions/ 1119335/javascript-local-variable-declare http:// stackoverflow。com/questions/598878/how-can-i-access-local-scope-dynamic-in-javascript – 2010-02-25 18:42:19