forEach 循环内的 JavaScript 变量范围 [英] JavaScript variable scope inside forEach loop

查看:59
本文介绍了forEach 循环内的 JavaScript 变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,有一个回调函数,forEach 循环遍历返回的结果.forEach 循环中的变量 'error' 和回调中的变量'error' 是否相同?

In the code below there is callback function used with forEach loop going over returned results. Is variable 'error' inside forEach loop and 'error' in callback same variables ?

session.getAll(options, function (error, varbinds) {
    varbinds.forEach(function (vb) {
        if (error) 
            console.log('getALL Fail ');
        else              
            console.log(vb.value);            
    });
});

推荐答案

是的,是同一个变量.

我不确定你知道多少.所以,我要详细解释一下.JavaScript 中的作用域是在函数级别*.将函数定义视为树上的点.树上的每个点都是一个范围.使用变量时,您只能使用当前范围内的内容以及向上到顶部(全局范围)的祖先可用的任何内容.这里有一些规则&可以帮助您更好地理解的示例:

I'm not sure how much you know. So, I'm going to explain in detail. Scoping in JavaScript is at the function level*. Think of function definitions as points on a tree. Each point on the tree is a scope. When using a variable, you can only use what is at your current scope and anything available to ancestors going up to the top (global scope). Here are a few rules & examples that may help you better understand:

*UPDATE: ES6 constlet 是块级

*UPDATE: ES6 const and let are block-level

内部函数可以访问外部函数级变量

function a() {
    var a = 4;

    function b() {
        alert(a); /* a = 4 */
    }
}

参数定义在相同的范围内,就好像它们在下面一行定义一样

function a(a) {
    // variable "a" is at same scope as the example above

    function b() {
        alert(a);
    }
}

相邻函数中的变量不可访问

函数 a() 是父函数.b() 和 c() 是它的孩子.这些孩子不能访问彼此的变量.

Function a() is the parent. b() and c() are its children. Those children cannot access each other's variables.

function a() {

    function b() {
        var aValue = 2;
    }

    function c() {
        alert(aValue); /* "aValue" is undefined here */
    }
}

函数定义的位置很重要

如果你运行 main();,这将返回 5:

This returns 5 if you run main();:

function getValue(returnFunc) {
    var a = 7;
    alert(returnFunc());
}

function main() {
    var a = 5;
    getValue(function() { return a; }); // anonymous function becomes "returnFunc"
}

最后,变量覆盖

(function getValue() {
    var a = 5;

    (function () {
        var a = 7;
        alert(a);
    })();

    alert(a);
})();

我试图避免在这些示例中使用自调用函数/IIFE,但在最后一个示例中我无法自拔.我认为这是最简单的方法.运行这个,你会得到 7,然后是 5.但是,如果你在那个内部的a"上排除了var"......

I tried to avoid using self-invoking functions/IIFEs for these examples but I just couldn't help myself on this last one. It's the easiest way, I think. Run this and you'll get 7, then 5. But, if you exclude "var" on that inner "a"...

(function getValue() {
    var a = 5;

    (function () {
        a = 7;
        alert(a);
    })();

    alert(a);
})();

你会得到 7、7.这是因为var"在内存中创建了一个新空间.此外,如果名称与更高范围内的某些内容发生冲突,它会被覆盖为不同的变量(尽管名称相同).

You'll get 7, 7. This is because "var" creates a new space in memory. Also, if there is a name conflict with something in a higher scope, it gets overridden as a different variable (despite having the same name).

更多示例,请参见:什么是范围JavaScript 中的变量?

这篇关于forEach 循环内的 JavaScript 变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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