Javascript:通过名称访问局部变量或闭包中的变量
大家好。
我们都知道,您可以使用 [] 语法通过名称访问 javascript 对象的属性。例如 ob['nameOfProperty']。
你能对局部变量做同样的事情吗?另一个答案此处建议答案是使用 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();
Possible Duplicate:
How can I access local scope dynamically in javascript?
Hi all.
We all know that you can access a property of a javascript object by it's name using the [] syntax.. e.g. ob['nameOfProperty'].
Can you do the same for a local variable? Another answer here suggested the answer is to use window['nameOfVar']. However, this only worked for the poster as he was defining variables at window-level scope.
I assume that this must be possible in general, as Firefox's Firebug (which I believe is written in javascript) can show local and closure variables. Is there some hidden language feature I'm not aware of?
Specifically, here's what I want to do:
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,这一般来说是不可能的。 JavaScript 使用作用域链解析函数内部的标识符;没有一个对象包含作用域中的所有变量,并且作用域链中的对象无法从页面中运行的 JavaScript 访问。
No, this isn't possible in general. JavaScript resolves identifiers inside a function using the scope chain; there's no single object that contains all the variables in scope, and the objects in the scope chain are inaccessible from JavaScript running in the page.
不可以,因为该变量只能从包含该变量作用域的执行上下文中访问。
换句话说,访问
j
的唯一方法是从test
范围内的某些内容访问它——通过内部函数或其他方式。这是因为j
从某种意义上说,对于范围更广的对象(如全局对象)来说并不存在。如果不是这样,那么变量名称必须是全局唯一的。No, because the variable is only accessible from within the execution contexts that contain the variable's scope.
Put another way, the only way you can access
j
is if you are accessing it from something in the scope oftest
-- via an inner function, or whatever. This is becausej
, in a sense, doesn't exist to objects with a wider scope, like the global object. If it were otherwise, then variable names would have to globally unique.我不知道 JavaScript 中内置了任何东西来引用类似的局部变量(尽管可能应该考虑所有变量都是由字符串内部引用的)。
如果您确实需要通过字符串访问,我建议将所有变量保留在一个对象中:
更新:这有点让我烦恼,因为没有办法像您想要的那样做到这一点。在内部,变量是声明性环境记录中的可变绑定。属性通过对象的环境记录绑定到它们所属的对象,但实际上有一种方法可以使用括号来访问它们。不幸的是,无法以相同的方式访问声明性环境记录。
I'm not aware of anything built into JavaScript to reference local variables like that (though there probably should be considering all variables are internally referenced by strings).
I'd suggest keeping all your variables in an object if you really need to access by string:
Update: It kind of bugs me that there's no way to do this like you want. Internally the variable is a mutable binding in the declarative environment records. Properties are bound to the object they're a property of through the object's environment records, but there's actually a way to access them using brackets. Unfortunately, there's no way to access the declarative environment records the same way.
使用 window 或任何其他全局访问器访问该变量将不起作用,因为该变量不可全局访问。但是您可以使用 eval 来计算任何表达式:
Accessing the variable with window or any other global accessor won't work because the variable is not globally accessible. But you can use can use eval to evaluate any expression:
怎么样:
What about: