Javascript function scoping and hoisting

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

    ES5: function hoisting & variable hoisting

    function hoisting priority is greater than variable hoisting

    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2016-06-01
     * @modified
     *
     * @description function-hoisting.js
     * @augments
     * @example
     * @link
     *
     */
    
    (function() {
      const log = console.log;
    
      var a = 1;
      function b() {
        a = 10;
        log(`local a`, a)
        return;
        // function hoisting priority is greater than variable hoisting
        function a() {}
      }
      b();
      log(`global a`, a);
      // local a 10
      // global a 1
    })();
    
    
    
    

    which is equal to

    (function() {
      const log = console.log;
    
      // define "a" in global scope
      var a = 1;
      function b() {
        // define "a" in local scope
        var a ;
        // assign function to a
        a = function () {};
        // overwrites local variable "a"
        a = 10;
        log(`local a`, a);
        return;
      }
    
      b();
      // log global variable "a"
      log(`global a`, a);
    
      // local a 10
      // global a 1
    })();
    
    

    the reason behind of hoisting

    var a = 1;                
    //"a" is global scope
    function b() {  
       var a = function () {}; 
       //"a" is local scope 
       var x = 12; 
       //"x" is local scope 
       a = 10;
       //global variable "a" was overwrited by the local variable "a"  
       console.log("local a =" + a);
       return console.log("local x = " + x);
    }       
    b();
    // local a =10
    // local x = 12
    console.log("global a = " + a);
    // global a = 1
    console.log("can't access local x = \n");
    // can't access local x = 
    console.log(x);
    // ReferenceError: x is not defined
    
    /**
     *  scpope & closure & hoisting (var/function)
     *  
     * 1. scpope : the global var can be access in any place(the whole file scope), local var only can be accessed by the local scope(function/block scope)!
     * Note: if a local variable not using var keywords in a function, it will become a global variable!
     * 
     * 2. closure : a function inner the other function, which can access local scope(parent function) & global scope, howerver it's vars can't be accessed by others! unless, your return it as return value!
     * 
     * 3. hoisting : move all declare/undeclare vars/function to the scope top, than assign the value or null!
     * Note: it just move the declare, not move the value!
     * 
     */
    
    

    ES6 let, const no exist hoisting

    (() => {
      const log = console.log;
      log(a)
      // Error: Uncaught ReferenceError: Cannot access 'a' before initialization
      let a = 1;
    })();
    
    
    
    
    (() => {
      const log = console.log;
      log(b)
      // Error: Uncaught ReferenceError: Cannot access 'b' before initialization
      const b = 1;
    })();
    
    

    refs

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

提交回复
热议问题