Javascript function scoping and hoisting

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

    Hoisting is behavioural concept of JavaScript. Hoisting (say moving) is concept that explains how and where variables should be declared.

    In JavaScript, a variable can be declared after it has been used because Function declarations and variable declarations are always moved (“hoisted”) invisibly to the top of their containing scope by the JavaScript interpreter.

    We encounter two types of hoisting in most cases.

    1.Variable declaration hoisting

    Lets understand this by this piece of code.

     a = 5; // Assign 5 to a
     elem = document.getElementById("demo"); // Find an element 
     elem.innerHTML = a;                     // Display a in the element
     var a; // Declare a
      //output-> 5
    

    Here declaration of variable a will be hosted to top invisibly by the javascript interpreter at the time of compilation. So we were able to get value of a. But this approach of declaration of variables is not recommended as we should declare variables to top already like this.

     var a = 5; // Assign and declare 5 to a
     elem = document.getElementById("demo"); // Find an element 
     elem.innerHTML = a;                     // Display a in the element
      // output -> 5
    

    consider another example.

      function foo() {
         console.log(x)
         var x = 1;
     }
    

    is actually interpreted like this:

      function foo() {
         var x;
         console.log(x)
         x = 1;
      }
    

    In this case x will be undefined

    It does not matter if the code has executed which contains the declaration of variable. Consider this example.

      function foo() {
         if (false) {
             var a = 1;
         }
         return;
         var b = 1;
      }
    

    This function turns out to be like this.

      function foo() {
          var a, b;
          if (false) {
            a = 1;
         }
         return;
         b = 1;
      }
    

    In variable declaration only variable definition hoists, not the assignment.

    1. Function declaration hoisting

    Unlike the variable hoisting the function body or assigned value will also be hoisted. Consider this code

     function demo() {
         foo(); // this will give error because it is variable hoisting
         bar(); // "this will run!" as it is function hoisting
         var foo = function () {
             alert("this would not run!!");
         }
         function bar() { 
             alert("this will run!!");
         }
     }
     demo();
    

    Now as we understood both variable and function hoisting, let's understand this code now.

    var a = 1;
    function b() {
      a = 10;
      return;
       function a() {}
    }
    b();
    alert(a);
    

    This code will turn out to be like this.

    var a = 1;                 //defines "a" in global scope
     function b() {  
       var a = function () {}; //defines "a" in local scope 
        a = 10;                 //overwrites local variable "a"
        return;      
     }       
     b();       
     alert(a); 
    

    The function a() will have local scope inside b(). a() will be moved to top while interpreting the code with its definition (only in case of function hoisting) so a now will have local scope and therefore will not affect the global scope of a while having its own scope inside function b().

提交回复
热议问题