<> Scope (Scope) The concept of
Scope refers to the scope of a variable , stay js in , There are two kinds of scopes .
* global scope .
* Function scope .
<> global scope
Written directly in script In label JS code .
* Global scopes are created when the page opens , Destroy on page close .
* There is a global variable in the global scope Window, It represents a browser window , It's created by the browser and we can use it directly .
In global scope :
* All variables created are treated as window Object property saving .
* All functions created will act as window How to save .
All variables in the global scope are global variables , It can be accessed in any part of the page .
<> Variable declaration in advance ( Variable promotion )
use var Keyword declared variables ( such as var a = 1), Will be declared before all code is executed ( But it's not assigned ), But if the variable is not declared var keyword ( For example, write directly a =
1), The variable is not declared .
give an example :
console.log(a); var a =1;
Print results :
The print result is undefined,( Explanatory variable a It was announced in advance , It's just that it hasn't been assigned )
<> Function is declared in advance
Function declaration
A function created in the form of a function declaration function foo(), Will be declared in advance .
in other words , The entire function is created before all the code is executed , So we can go ahead of the function declaration , Call function .
Code examples :
fn1(); function fn1(){ document.write(" I'm a function fn1"); }
Print results
<> Function expression
Functions created using function expressions var foo = function(){}, Will not be declared in advance
, So it can not be called before the declaration . Because at this time foo It was declared , And undefined, I didn't take it function() Assign to foo.
<> Scope
Scope : The area in which variables and functions take effect , Scope in function definition , That's it .
Access variables in global scope in function scope , Variables in the function scope cannot be accessed in the global scope .
Runtime context : When the function executes
Time , An internal object of the runtime context is created , Every time the function is called , You will create a new context object , They are independent of each other , When the function is finished , The runtime context it generates is destroyed .
( But when a variable inside a function is referenced by another function , Then the variables of this function will not be destroyed )
Scope hierarchy :
When you operate on a variable in the scope of a function , It first looks in its own scope , If so, use it directly ( Principle of proximity
). If not, look up the scope , Until the global scope is found ; If it is still not found in the global scope , You will report an error ReferenceError.
To access global variables in a function, you can use the window object ,( for instance , Both global scope and function scope define variables a, If you want to access global variables , have access to window.a)
remind 1:
There is also a feature of early declaration in function scope :
* use var Keyword declared variables , Functions are declared before all code is executed
* Function declarations are also executed before all code in the function is executed
therefore , in function , No, var Declared variables are global variables , And it won't be announced in advance .
give an example :
var a = 1; function foo(){ console.log(a); a = 2; // Here a amount to window.a, Automatically declare as a global variable }
foo(); console.log(a); // The print result is 2
Print results :
In the code above ,foo() The result of the print is 1, If you take out the first line of code , The print result is
remind 2: Defining formal parameters is equivalent to declaring variables in the function scope .
for example ;
function fun6(e){// This function has formal parameters e, At this point, it is equivalent to writing in the first line of code inside the function var e; console.log(e); }
fun6();// The print result is undefined; fun6(123);// The print result is 123;
Print results :
Technology