有没有办法获得具有相同名称的外部作用域变量? [英] Is there a way to get outer scope variables with the same name?

查看:38
本文介绍了有没有办法获得具有相同名称的外部作用域变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在外部闭包中定义了某个变量,而在内部闭包中定义了另一个同名的变量.我能以某种方式获得外部变量吗?

If I defined some variable in outer closure, and in the inner closure has defined another variable with the same name. Can I somehow get the outer variable?

我知道它仍然在内存中的某个地方并且没有被覆盖,因为它可以在函数之后打印.有没有办法访问它?

I know it's still somewhere in the memory and not been overwritten since it can be printed after the function. Is there a way to access it?

const foo = 'outer';

function bar() {
  const foo = 'inner';

  // Is there a way to get the outside foo from here?
  console.log(foo);   // "inner", but I want it to be "outer"
}

bar();

console.log(foo); // "outer"

推荐答案

如果 foo 在顶层并且使用 const 声明,则访问的唯一方法之一它带有 new Function,运行时的作用域在顶层.(请不要真的这样做):

If foo is on the top level and declared with const, one of the only ways to access it with new Function, whose scope when run is on the top level. (Please don't actually do this):

const foo = 'outer';

function bar() {
  const foo = 'inner';
  const fn = new Function('return foo');
  console.log(fn('foo'));
}

bar();

console.log(foo); // "outer"

见下面 Hao 的评论.eval 也可以,但稍微复杂一些.正如 MDN 所说:

See comment by Hao below. eval can work too, but it's a bit more complicated. As MDN says:

如果您间接使用 eval 函数,通过引用而不是 eval 调用它,那么从 ECMAScript 5 开始,它在全局范围内工作,而不是在本地范围内.这意味着,例如,函数声明会创建全局函数,并且被评估的代码无权访问调用它的范围内的局部变量.

If you use the eval function indirectly, by invoking it via a reference other than eval, as of ECMAScript 5 it works in the global scope rather than the local scope. This means, for instance, that function declarations create global functions, and that the code being evaluated doesn't have access to local variables within the scope where it's being called.

因此,如果您通过调用独立变量名称 eval(arg) 以外的任何方法引用 eval,代码将在顶层,它将能够看到顶层的变量:

So if you reference eval by any method other than calling the standalone variable name eval(arg), the code will run on the top level, and it will be able to see the variable on the top level:

const foo = 'outer';

function bar() {
  const foo = 'inner';
  console.log(window.eval('foo'));
  // Another method:
  console.log((0, eval)('foo'))
  // Another method:
  const anotherEvalReference = eval;
  console.log(anotherEvalReference('foo'));
}

bar();

console.log(foo); // "outer"

如果 foo 是用 var 而不是 const 声明的,你就可以访问全局对象的那个属性:

If foo is declared with var instead of const, you can just access that property of the global object:

var foo = 'outer';

function bar() {
  const foo = 'inner';
  console.log(window.foo);
}

bar();

console.log(foo); // "outer"

如果阴影变量不是在顶层,例如

If the shadowed variable is not on the top level, eg

(() => {
  const foo = 'intermediate';
  (() => {
    const foo = 'inner';
    // more code here
  })();
})();

那么就无法从 more code here 部分访问 intermediate 变量.

Then there's no way to access the intermediate variable from the more code here section.

这篇关于有没有办法获得具有相同名称的外部作用域变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆