最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript variable scope inside forEach loop - Stack Overflow

programmeradmin2浏览0评论

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);            
    });
});

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);            
    });
});
Share Improve this question edited Oct 15, 2015 at 17:21 rlemon 17.7k14 gold badges94 silver badges126 bronze badges asked Oct 15, 2015 at 17:19 iromirom 3,59617 gold badges58 silver badges92 bronze badges 4
  • 2 unless the inner function scope introduces a new 'error' variable, it will be the same. – rlemon Commented Oct 15, 2015 at 17:22
  • 1 You don't have any other error variable defined, so the answer is yes. – Artyom Neustroev Commented Oct 15, 2015 at 17:22
  • 1 As others have said yes if it isn't re-defined with a var in the loop, however a forEach loop cannot be aborted. So checking the error should be done in the callback before you get into the loop. If you need to check in the loop for whatever reason a different looping method should be employed. – ste2425 Commented Oct 15, 2015 at 17:24
  • @irom I realize this is a few years old now, but I just got an upvote and, when I checked it out, I saw no accepted answer here. Did we manage to help enough to check this off? – BCDeWitt Commented Jun 1, 2018 at 19:40
Add a comment  | 

2 Answers 2

Reset to default 11

Yes, it is the same variable.

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 const and let are block-level

Inner functions have access to outer function-level variables

function a() {
    var a = 4;

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

Parameters are defined at the same scope as if they were defined one line below

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

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

Variables in adjacent functions are not accessible

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 */
    }
}

Location of function definition is what counts

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"
}

Lastly, variable overriding

(function getValue() {
    var a = 5;

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

    alert(a);
})();

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);
})();

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).

For some more examples, please see: What is the scope of variables in JavaScript?

Yes, it's the same variable, it would change if you define another error variable inside the scope of the forEach callback by using the var keyword:

session.getAll(options, function (error, varbinds) {
    varbinds.forEach(function (vb) {
        if (error) //Same error as the error parameter above
            console.log('getALL Fail ');
        else              
            console.log(vb.value);            
    });
});

session.getAll(options, function (error, varbinds) {
    varbinds.forEach(function (vb) {
        var error = false; //New error for this closure.
        if (error) 
            console.log('getALL Fail ');
        else              
            console.log(vb.value);            
    });
});
发布评论

评论列表(0)

  1. 暂无评论