Javascript function scoping and hoisting

后端 未结 18 2952
孤街浪徒
孤街浪徒 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 04:58

    Its all depends on the scope of variable 'a'. Let me explain by creating scopes as images.

    Here JavaScript will create 3 scopes.

    i) Global scope. ii) Function b() scope. iii) Function a() scope.

    Its clear when you call 'alert' method scope belongs to Global that time, so it will pick value of variable 'a' from Global scope only that is 1.

提交回复
热议问题