Global or Closure?

后端 未结 2 887
我寻月下人不归
我寻月下人不归 2021-01-24 10:54

\"Console

As per my understanding variable \'a\' is a global variable. But when I check this in Chrome cons

2条回答
  •  再見小時候
    2021-01-24 11:47

    Yes it shows in Chrome but not in Mozilla.

    First you need to understand lexical scoping in JavaScript. It means that the inner functions have access to the variables and other resources of their parent scope. Here the parent scope is global scope.

    This is how the function adds a variable defined outside the function, inside it using closure. In order to execute the function, it will look for the variables it need. Variable 'a' in not defined inside so it will look at scope outside the function till it finds it.

    This is a closure.

提交回复
热议问题