Javascript function scoping and hoisting

后端 未结 18 2996
孤街浪徒
孤街浪徒 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: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.

    
    

    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

     
    

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

提交回复
热议问题