The JavaScript variable scope

Scope variables:
the so-called variable scope, is variable range
of each variable has its own scope
scope into two
variables defined in the function called local scope / local variables
include the parameters of the function , defined inside the function variable, have become the local scope / local variables
can only be used inside a function call
function can not be called directly outside
as long as the external function call variables inside a function, it is being given
defined outside the function the variable is called global scope / global variables
inside the function can call the global scope of the variable, but is conditional
function call variable, the variable assignment principle of
principles calling variables
will be in the current scope, to find whether there this variable
If so, use the variable directly
If not, go looking for parent scoped variable
if the parent has a variable scope, on the use of this variable
if this variable is not the parent scope, looking in one direction
if all the scope of this variable are not, the results are given
only Looking to the parent scope, not to look for a subset of the scope of
the variable assignment principle
to find the variables in the current scope, if so, this variable assignment
if the current scope without this variable, we went to the parent role looking for this variable domain and then assign to it
if all scopes do not have this variable, where the assignment will be upgraded to a statement declaring variables
and the definition of a global scope variables, functions can be called directly on the outside
will not go looking for a subset variable assignment
Note: The definition of variables, be sure to write keyword var, whether global or local

Published 21 original articles · won praise 3 · Views 326

Guess you like

Origin blog.csdn.net/qq_44531034/article/details/104934443