javascript 如何在JS中的嵌套内部函数中访问外部函数变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32036408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:45:23  来源:igfitidea点击:

How to access outer function variable in nested inner function in JS

javascript

提问by Roy

I am new to JS and having a doubt with the below example. Please see the inline comments.

我是 JS 新手,对下面的例子有疑问。请参阅内嵌评论。

function outer() {
 var x = 5;
 console.log("outer",x); // Prints 5
 console.log("-----------");
 function inner() {
  var x = 6;
  console.log("inner",x); // Prints 6
  console.log("outer",x); // Prints 6. How to print 5
  console.log("-----------");
  function _inner() {
   var x = 7;
   console.log("_inner",x); // Prints 7
   console.log("inner",x); // Prints 7. How to print 6
   console.log("outer",x); // Prints 7. How to print 5
   console.log("-----------");
  }
  _inner();
 }
 inner();
}
outer();

采纳答案by Blauharley

May be this helps you. Assign your variables to your nested functions because therefore it is clear what varible should be used but they have to differ from each other in some way(name or via namespace):

可能这对你有帮助。将变量分配给嵌套函数,因为因此很清楚应该使用什么变量,但它们必须以某种方式彼此不同(名称或通过命名空间):

function outer() {
    var x = 5;
    // or via namespace
    // var out = { x : 5 };
    console.log("outer",x); // 5
    console.log("-----------");
    function inner() {
        inner.x = 6;
        console.log("inner",inner.x); // 6
        console.log("outer",x); // 5
        console.log("-----------");
        function _inner() {
            _inner.x = 7;
            console.log("_inner",_inner.x); // 7
            console.log("inner",inner.x); // 6
            console.log("outer",x); // 5
            // namespace 
            // console.log("outer",out.x); // 5
            console.log("-----------");
        }
        _inner();
    }
    inner();
}
outer();

In this example only two of three varibles are assign(not outer x) to functions because otherwise you could assess outer.x from outside outer function and assign it any value:

在这个例子中,只有三个变量中的两个被赋值(不是外部 x)给函数,否则你可以从外部函数评估 outer.x 并为其分配任何值:

function outer(){
   outer.x = 5;
   ...
}
// assign any value outside outer function
outer.x = 34;

But when a local variable is defined:

但是当定义了局部变量时:

function outer(){
   var x = 23;
}

Then there is no chance to assign this local variable(x) any value from outside outer function.

那么就没有机会从外部函数外部为这个局部变量 (x) 分配任何值。

回答by Axel Mani

Javascript doesn't by default have blocked scope variable like many programming languages. This means that the variable x like you declared above, is the same variable.

默认情况下,Javascript 不像许多编程语言那样具有阻塞作用域变量。这意味着变量 x 就像你在上面声明的那样,是同一个变量。

Variable declarations, wherever they occur, are processed before any code is executed. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

变量声明,无论它们出现在哪里,都会在执行任何代码之前进行处理。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

I would suggest to not use the same variable name in a nested function. It becomes hard to read and debug. Also do you really need to have nested function ?

我建议不要在嵌套函数中使用相同的变量名。它变得难以阅读和调试。你真的需要嵌套函数吗?

function outer() {
    var x = 5;
    console.log("outer",x); // Prints 5
    console.log("-----------");
    inner(x);
}
function inner(x1) {
    var x = 6;
    console.log("inner",x); // Prints 6
    console.log("outer",x1); // Prints 5
    console.log("-----------");
    _inner(x,x1);
}
function _inner(x1, x2) {
    var x = 7;
    console.log("_inner",x); // Prints 7
    console.log("inner",x1); // Prints 6. 
    console.log("outer",x2); // Prints 5.
    console.log("-----------");
}
outer();

Or you could use object declaring it like so

或者你可以像这样使用对象声明它

function outer() {
    var x = {
      outer: 5
    };
    console.log("outer",x,outer); // Prints 5
    console.log("-----------");
    function inner() {
        x.inner = 6;
        console.log("inner",x.outer); // Prints 6
        console.log("outer",x.inner); // Prints 6. How to print 5
        console.log("-----------");
        function _inner() {
            x._inner = 7;
            console.log("_inner",x._inner); // Prints 7
            console.log("inner",x.inner); // Prints 7. How to print 6
            console.log("outer",x.outer); // Prints 7. How to print 5
            console.log("-----------");
        }
        _inner();
    }
    inner();
}
outer();

回答by ManishSingh

when we reuse the variable name inside an method then only block-scope will work. means scope will be work. Means for these variables, the braces {. . .} will define the scope.

当我们在方法中重用变量名时,只有块范围才能工作。意味着范围将起作用。对于这些变量,大括号 {. . .} 将定义范围。

what you can try is define Global Variables

你可以尝试的是 定义全局变量

var x = 5;
    var innerx = 5;
    var _innerx = 5;
    function outer() {
    }