Scope:
Variables also have a range of function functions.
The browser's kernel has two main functions, one is the rendering page and the other is our JavaScript interpreter.
We mainly talk about how the JavaScript interpreter works in parsing.
At the time of parsing, pre-parsing occurs first, that is, it does not go directly to the line to execute the code we write, it will first find out what the declared variables, meet the function body when the function is placed in the top position, when the variable name and function name when the function will replace the variable declaration.
When parsing is a function body, the variables or functions in the function body are pre-processed, knowing what is declared.
If it is a variable, then the variable in the function body is called, and if not in the function body, the outer variable is found
Function Pre-parsing:
Will refer to the top of the function
Variable Pre-parsing:
Will know beforehand if there is a variable, but will not change the parse location
When pre-parsing, when the variable and function have the same name, the function overrides the variable, and the same name only has one
Global scope-accessible from anywhere
Variables defined outside the function have global scope
Variables that are not defined with VAR have global scope
Properties on all Window objects have global scope
Local scope-can only be accessed inside a function
Use Var to have local scope for variables defined inside a function
Global: can be called from anywhere
Local: Can be called only in the current scope (functional body)
1<script>2 varA = 10;//Global3 functionfn () {4Console.log (a);//undefined5 varA = 5;//Local6Console.log (a);//57 }8 fn ();9 Ten</script>
The concept of closures
function nesting function, the inner layer function can access the variables in the outer function
1<script>2 functionfn1 () {3 varA = 1;4 functionfn2 () {5a++;6 Console.log (a);
return A;7 }8 fn2 ();9 }Ten fn1 (); One</script>
There are two big uses for closures:
One is to read the variables inside of I outside the function body, and the other is to keep the variables in memory.
---scopes in javascript