Javascript:通过名称访问局部变量或闭包中的变量

发布于 2024-08-23 04:00:23 字数 865 浏览 8 评论 0原文

可能的重复:
如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

百善笑为先 2024-08-30 04:00:24

不,这一般来说是不可能的。 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.

失去的东西太少 2024-08-30 04:00:24

不可以,因为该变量只能从包含该变量作用域的执行上下文中访问。

换句话说,访问 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 of test -- via an inner function, or whatever. This is because j, 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.

我们的影子 2024-08-30 04:00:23

我不知道 JavaScript 中内置了任何东西来引用类似的局部变量(尽管可能应该考虑所有变量都是由字符串内部引用的)。

如果您确实需要通过字符串访问,我建议将所有变量保留在一个对象中:

var variables = {
    "j": 1
};
alert(variables["j"]);

更新:这有点让我烦恼,因为没有办法像您想要的那样做到这一点。在内部,变量是声明性环境记录中的可变绑定。属性通过对象的环境记录绑定到它们所属的对象,但实际上有一种方法可以使用括号来访问它们。不幸的是,无法以相同的方式访问声明性环境记录。

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:

var variables = {
    "j": 1
};
alert(variables["j"]);

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.

櫻之舞 2024-08-30 04:00:23

使用 window 或任何其他全局访问器访问该变量将不起作用,因为该变量不可全局访问。但是您可以使用 eval 来计算任何表达式:

<script>
function test(){
   var j = 1; 
   alert(eval("j"));
}

test();
</script>

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:

<script>
function test(){
   var j = 1; 
   alert(eval("j"));
}

test();
</script>
撕心裂肺的伤痛 2024-08-30 04:00:23

怎么样:

<script>
    function Func(){
        var fn = arguments.callee;
        fn.j = 99;
        console.log(fn['j']);
    }
    Func();
    console.log(window['j']); //not global
    console.log(Func['j']); //but not private
</script>

What about:

<script>
    function Func(){
        var fn = arguments.callee;
        fn.j = 99;
        console.log(fn['j']);
    }
    Func();
    console.log(window['j']); //not global
    console.log(Func['j']); //but not private
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文