Javascript function scoping and hoisting

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

    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!

    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

提交回复
热议问题