简体   繁体   English

我可以显式引用被同名本地阴影的JavaScript全局变量吗?

[英]Can I explicitly reference a JavaScript global variable that's been shadowed by a local with the same name?

I've seen tons of posts about the difference between global and function scope in JavaScript, far too many to link here. 我已经看到很多关于JavaScript中全局和功能范围之间差异的帖子,这里有很多链接。 I've also seen my exact question asked about Python . 我也看到了关于Python的确切问题。 So what I want to know is, how can I access a global variable when a "closer" scope also has a variable with the same name? 所以我想知道的是,当“更接近”的范围也有一个具有相同名称的变量时,如何访问全局变量?

var a = "global";
function b(){
  var a = "local";
  var magic = ...; // somehow put "global" in magic
  console.log(magic); // should print "global"
}

In browser only , I figured out that you can use window.a to specify the global. 仅在浏览器中 ,我发现您可以使用window.a来指定全局。 Is there anything that works server-side as well? 服务器端有什么用吗?

If it's really a global (ie not just in the outer scope), you can do this : 如果它真的是全局的(即不只是在外部范围内),你可以这样做:

var magic = (function(name){return this[name]}).call(null, "a");

From ECMAScript's documentation (10.4.3) : 从ECMAScript的文档(10.4.3)

Entering Function Code 输入功能代码

The following steps are performed when control enters the execution context for function code contained in function object F, a caller provided thisArg, and a caller provided argumentsList: 当控件进入函数对象F中包含的函数代码的执行上下文,调用者提供thisArg和调用者提供的argumentsList时,执行以下步骤:

If the function code is strict code, set the ThisBinding to thisArg. 如果函数代码是严格代码,请将ThisBinding设置为thisArg。 Else if thisArg is null or undefined, set the ThisBinding to the global object. 否则,如果thisArg为null或未定义,则将ThisBinding设置为全局对象。

Note that you can't test this in jsFiddle, as the global scope isn't where you define your a. 请注意,您无法在jsFiddle中对此进行测试,因为全局范围不是您定义a的位置。

Demonstration : 示范

 var a = "global"; function b(){ var a = "local"; var magic = (function(name){return this[name]}).call(null, "a"); document.body.textContent = 'a: '+JSON.stringify(magic); // print "global" } b(); 

There is no standard to do that. 没有标准可以做到这一点。 For my own purposes, I always put my variables in a container where the name resembles the project name: 出于我自己的目的,我总是将我的变量放在容器中,其名称类似于项目名称:

var PROJECT = {}; var PROJECT = {}; // Put all "globals" for "project" into this container //将“project”的所有“全局变量”放入此容器中

That way, I can collect all variables in one place and if I need to pass the "globals" around, I have to pass at most one reference. 这样,我可以在一个地方收集所有变量,如果我需要传递“全局”,我必须通过最多一个引用。

Using your code as the basis of the answer, you could use the following to actually pull the global version of " a " as opposed to the local version: 使用您的代码作为答案的基础,您可以使用以下内容实际拉出“ a ”的全局版本而不是本地版本:

var a = "global";
function b(){
  var a = "local";
  var magic = this.a; // somehow put "global" in magic
  console.log(magic); // should print "global"
}
b();

This will only work if you are calling the b() function in the form above. 只有在上面的表单中调用b()函数时,这才有效。 An example in which this will not work is as follows: 这不起作用的一个例子如下:

var a = "global";
function b(){
  var a = "local";
  var magic = this.a; // somehow put "global" in magic
  console.log(magic); // should print "global"
}
b.call({a : "other context"});

In the case above, "other context" will be printed to the console. 在上面的情况中, "other context"将打印到控制台。

well I don´t have a direct answer, but a workaround is to use a well established container, window for example 好吧,我没有直接的答案,但解决方法是使用一个完善的容器,例如窗口

window['a'] = "global";

This isn't a very clean solution but it does the job 这不是一个非常干净的解决方案,但它可以完成这项工作

You can access the global through the global object. 您可以通过全局对象访问全局。 In this case window and the local by name inside the current context. 在这种情况下窗口和当前上下文中的本地名称。

Here is an fiddle example . 这是一个小提琴的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM