Javascript function scoping and hoisting

后端 未结 18 2925
孤街浪徒
孤街浪徒 2020-11-21 04:20

I just read a great article about JavaScript Scoping and Hoisting by Ben Cherry in which he gives the following example:

var a = 1;

function b() {
    a =          


        
相关标签:
18条回答
  • 2020-11-21 05:10
    1. function declaration function a(){} is hoisted first and it behaves like var a = function () {};, hence in local scope a is created.
    2. If you have two variable with same name (one in global another in local), local variable always get precedence over global variable.
    3. When you set a=10, you are setting the local variable a , not the global one.

    Hence, the value of global variable remain same and you get, alerted 1

    0 讨论(0)
  • 2020-11-21 05:12

    It is happening because of the Variable name is same as the function name means "a". Thus due to Javascript hoisting it try to solve the naming conflict and it will return a = 1.

    I was also confused about this until i read this post on "JavaScript Hoisting" http://www.ufthelp.com/2014/11/JavaScript-Hoisting.html

    Hope it helps.

    0 讨论(0)
  • 2020-11-21 05:13

    What is the bone of contention in this small snippet of code?

    Case 1:

    Include function a(){} definition inside the body of function b as follows. logs value of a = 1

    var a = 1;
    function b() {
      a = 10;
      return;
    
      function a() {}
    }
    b();
    console.log(a); // logs a = 1
    

    Case 2

    Exclude function a(){} definition inside the body of function b as follows. logs value of a = 10

    var a = 1;
    function b() {
      a = 10;  // overwrites the value of global 'var a'
      return;
    }
    b();
    console.log(a); // logs a = 10
    

    Observation will help you realise that statement console.log(a) logs the following values.

    Case 1 : a = 1

    Case 2 : a = 10

    Posits

    1. var a has been defined and declared lexically in the global scope.
    2. a=10 This statement is reassigning value to 10, it lexically sits inside the function b.

    Explanation of both the cases

    Because of function definition with name property a is same as the variable a. The variable a inside the function body b becomes a local variable. The previous line implies that the global value of a remains intact and the local value of a is updated to 10.

    So, what we intend to say is that the code below

    var a = 1;
    function b() {
      a = 10;
      return;
    
      function a() {}
    }
    b();
    console.log(a); // logs a = 1
    

    It is interpreted by the JS interpreter as follows.

    var a = 1;
    function b() {
      function a() {}
      a = 10;
      return;
    
    
    }
    b();
    console.log(a); // logs a = 1
    

    However, when we remove the function a(){} definition, the value of 'a' declared and defined outside the function b, that value gets overwritten and it changes to 10 in case 2. The value gets overwritten because a=10 refers to the global declaration and if it were to be declared locally we must have written var a = 10;.

    var a = 1;
    function b() {
      var a = 10; // here var a is declared and defined locally because it uses a var keyword. 
      return;
    }
    b();
    console.log(a); // logs a = 1
    

    We can clarify our doubt further by changing the name property in function a(){} definition to some other name than 'a'

    var a = 1;
    function b() {
      a = 10; // here var a is declared and defined locally because it uses a var keyword. 
      return;
    
      function foo() {}
    }
    b();
    console.log(a); // logs a = 1
    
    0 讨论(0)
  • 2020-11-21 05:13

    Hoisting is a concept made for us to make it easier to understand. What actually happens is the declarations are done first with respect to their scopes and the assignments will happen after that(not at the same time).

    When the declarations happen, var a, then function b and inside that b scope, function a is declared.

    This function a will shadow the variable a coming from the global scope.

    After the declarations are done, the values assign will start, the global a will get the value 1 and the a inside function b will get 10. when you do alert(a), it will call the actual global scope variable. This little change to the code will make it more clear

            var a = 1;
    
        function b() {
            a = 10;
            return a;
    
            function a() { }
        }
    
        alert(b());
        alert(a);
    
    0 讨论(0)
  • 2020-11-21 05:13

    Suprisingly, none of the answers here mention the relevancy of the Execution Context in the Scope Chain.

    The JavaScript Engine wraps the currently executing code in an Execution Context. The base execution context is the global Execution Context. Each time a new function is invoked, a new Execution Context is created and put on the Execution Stack. Think of a Stack Frame sitting on an Invocation Stack in other programming languages. Last in first out. Now each Execution Context has its own Variable Environment and Outer Environment in JavaScript.

    I will use the below example as a demonstration.

    1) First, we enter the Creation Phase of the global Execution Context. Both the Outer Environment and Variable Environment of the Lexical Environment are created. The Global Object is setup and placed in memory with the special variable 'this' pointing to it. The function a and its code and the variable myVar with an undefined value are placed in memory in the global Variable Environment. it's important to note that function a's code is not executed. It is just placed in memory with function a.

    2) Second, it is the Execution Phase of the Execution Context. myVar is no longer an undefined value. It is initialized with value of 1, which is stored in the global Variable Environment. The function a is invoked and a new Execution Context is created.

    3) In the function a's Execution Context, it goes through the Creation and Execution Phase of its own Execution Context. It has its own Outer Environment and Variable Environment, thus, its own Lexical Environment. The function b and the variable myVar are stored in its Variable Environment. This Variable Environment is distinct from the global Variable Environment. Since the function a sits lexically (physically in code) on the same level as the global Execution Context, its Outer Environment is the global Execution Context. Thus, if the function a was to refer to a variable that is not in its Variable Environment, it will search the Scope Chain and try to find the variable in the Variable Environment of the global Execution Context.

    4) The function b is invoked in function a. A new Execution Context is created. Since it sits lexically in function a, its Outer Environment is a. So when it references myVar, since myVar is not in function b's Variable Environment, it will look in function a's Variable Environment. It finds it there and console.log prints 2. But if the variable was not in function a's Variable Environment, then since function a's Outer Environment is the global Execution Context, then the Scope Chain will continue searching there.

    5) After function b and a are finished execution, they are popped from the Execution Stack. The single-threaded JavaScript Engine continues execution at the global Execution Context. It invokes the b function. But there is no b function in the global Variable Environment and there is no other Outer Environment to search in the global Execution Context. Thus an exception is raised by the JavaScript Engine.

    function a(){
      function b(){
        console.log(myVar);
      }
    
      var myVar = 2;
      b();
    }
    
    var myVar = 1;
    a();
    b();
    > 2
    > Uncaught ReferenceError: b is not defined
    

    The below example shows the Scope Chain in action. In the function b's Execution Context's Variable Environment, there is no myVar. So it searches its Outer Environment, which is the function a. The function a does not have myVar in its Variable Environment either. So the Engine searches function a's Outer Environment, which is the global Execution Context's Outer Environment and myVar is defined there. Hence, the console.log prints 1.

    function a(){
      function b(){
        console.log(myVar);
      }
    
      b();
    }
    
    var myVar = 1;
    a();
    > 1
    

    Regarding Execution Context and the Lexical Environment associated with it, including Outer Environment and Variable Environment, enable the scoping of variables in JavaScript. Even if you invoke the same function multiple times, for each invocation, it will create its own Execution Context. So each Execution Context will have its own copy of the variables in its Variable Environment. There is no sharing of variables.

    0 讨论(0)
  • 2020-11-21 05:14

    To describe hosting in javascript in one sentence is variables and functions are hoisted to the top of the scope that they are declared in.

    I am assuming you are a beginner, to understand hoisting properly at first we have understood the difference between undefined and ReferenceError

     var v;
     console.log(v);
     console.log(abc);
    /*
    The output of the above codes are:
    undefined
    ReferenceError: abc is not defined*/
    

    now in the bellow code what we see? a variable and a function expression is decleard.

    <script>
    var totalAmo = 8;
    var getSum = function(a, b){
          return a+b;
    }
    </script>
    

    but the real picture with proof that the both variable and function are hoisted on the top of there scope:

    console.log(totalAmo);
    console.log(getSum(8,9));
    var totalAmo = 8;
    var getSum = function(a, b){
          return a+b;
    }
    console.log(totalAmo);
    console.log(getSum(9,7));
    

    Output of first two logs are undefined and TypeError: getSum is not a function because both var totalAmo and getSum are hoisted on the top of their scope like bellow

     <script>
            var totalAmo;
            var getSum;
    
            console.log(totalAmo);
            console.log(getSum(8,9));
            var totalAmo = 8;
            var getSum = function(a, b){
                return a+b;
            }
            console.log(totalAmo);
            console.log(getSum(9,7));
        </script>
    

    But for functions declaration whole functions hoisted on the top of their scope.

    console.log(getId());
    function getId(){
       return 739373;
    }
    /* output: 739373, because the whole function hoisted on the top of the scope.*/
    

    Now the same logic goes for those varibale, functions experessions and function declaratoins declared inside functional scope. Key point: they will not be hoisted on the top of the file;

    function functionScope(){
                var totalAmo;
                var getSum;
    
                console.log(totalAmo);
                console.log(getSum(8,9));
                var totalAmo = 8;
                var getSum = function(a, b){
                    return a+b;
                }
            }
    

    So, when you use var keyword, variable and function hoisted on the top of there scope (global scope and function scope). What about let and const, const and let are still both aware of the global scope and function scope just like var is, but const and let variables are also aware of another scope called blocked scope. a block scope is present whenever there is a block of code, such as for loop, if else statement, while loop etc.

    When we use const and let to declare a variable in these block scope, the variable declaration only will be hoisted on the top of that block that it is in, and it will not be hoisted on the top of the parent function or top of the global scope that it is hoisted.

     function getTotal(){
                let total=0;
                for(var i = 0; i<10; i++){
                    let valueToAdd = i;
                    var multiplier = 2;
                    total += valueToAdd*multiplier;
                }
                return total;
            }
    

    Variables in abobe example will be hoisted like bellow

     function getTotal(){
                let total;
                var multiplier;
                total = 0;
                for(var i = 0; i<10; i++){
                    let valueToAdd;
                    valueToAdd = i;
                    multiplier = 2;
                    total += valueToAdd*multiplier;
                }
                return total;
            }
    
    0 讨论(0)
提交回复
热议问题