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

Clarification on nested function variable scope within javascript - Stack Overflow

programmeradmin1浏览0评论

This seems to be a topic that has been asked several times before but i'm really struggling to find a succinct explanation. Closest I could find was this (What is the scope of variables in JavaScript?) but there are several aspects I still don't quite understand. As you have probably guessed I'm fairly new to javascript but this aspect seems crucial to understand fully if I am ever going to get anywhere with it.

So consider a program prised of several nested functions:

function functionA() {
    var functionAVariable = "A";
}

function functionB() {
    var functionBVariable = "B";
}

function functionC() {
    var functionCVariable = "C";

    function functionD() {
        var functionDVariable = "D";
    }
}

var globalVariable = "G";
var mainFunction = function () {
var mainFunctionVariable = "M";
    functionA();        
    functionB();
    functionC();
};

From my understanding all functions will have access to globalVariable and functionA, functionB and functionC will have access to mainFunctionVariable.

Where I start to get a bit confused is when we add deeper nests to the overall program as with functionD. Will functionD have access to all higher level function variable that currently wrap it or will it only have access to the immediate parent, functionC?

This seems to be a topic that has been asked several times before but i'm really struggling to find a succinct explanation. Closest I could find was this (What is the scope of variables in JavaScript?) but there are several aspects I still don't quite understand. As you have probably guessed I'm fairly new to javascript but this aspect seems crucial to understand fully if I am ever going to get anywhere with it.

So consider a program prised of several nested functions:

function functionA() {
    var functionAVariable = "A";
}

function functionB() {
    var functionBVariable = "B";
}

function functionC() {
    var functionCVariable = "C";

    function functionD() {
        var functionDVariable = "D";
    }
}

var globalVariable = "G";
var mainFunction = function () {
var mainFunctionVariable = "M";
    functionA();        
    functionB();
    functionC();
};

From my understanding all functions will have access to globalVariable and functionA, functionB and functionC will have access to mainFunctionVariable.

Where I start to get a bit confused is when we add deeper nests to the overall program as with functionD. Will functionD have access to all higher level function variable that currently wrap it or will it only have access to the immediate parent, functionC?

Share Improve this question edited May 23, 2017 at 11:43 CommunityBot 11 silver badge asked Aug 7, 2015 at 12:01 AshAsh 4448 silver badges27 bronze badges 1
  • Variable scope in nested functions in Javascript – ozil Commented Aug 7, 2015 at 12:06
Add a ment  | 

2 Answers 2

Reset to default 10

From my understanding all functions will have access to globalVariable

Correct

and functionA, functionB and functionC will have access to mainFunctionVariable.

No. Only the anonymous function assigned to mainFunction will have access to mainFunctionVariable. Scope is determined by where a function is created, not where it is called.

Where I start to get a bit confused is when we add deeper nests to the overall program as with functionD. Will functionD have access to all higher level function variable that currently wrap it or will it only have access to the immediate parent, functionC?

A function has access to all variables in its own scope and in wider scopes (unless they are masked by another variable of the same name).

var x = 1;

function a() {
  var y = 2;

  function b() {
    var z = 3;

    function c() {
      alert([x, y, z]);
    }
    c();
  }
  b();
}
a();

Scope of the variable is only for function itself in which it is declared. If we need to access the variable of one function into another then we have to pass it as a parameter of the function in which we need to access otherwise we have to create global variable, global variable is accessible in all functions of script.

In example above

functionAVariable is accessible only in functionA, 
functionBVariable is accessible only in functionB and
functionCVariable is accessible only in functionC. 

You can not access functionCVariable in functionD same as mainFunctionVariable is only accessible in mainFunction. mainFunctionVariable is not accessible in functionA, functionB, functionC.

Variable globalVariable is accessible in all the functions.

If you declare variable without specifying any datatype like var then it is by default treated as global variable. lets say we have functionA like

function functionA() {
    var functionAVariable = "A";
    globVariable = 10;
}

in above snippet, functionAVariable is only accessible in functionA but globVariable is now behave like global variable.

发布评论

评论列表(0)

  1. 暂无评论