When local and global variables of the same name, hides the global variable;
4. Variable
Keywords: 4. Variable
4.1 Types of variables
JS is a loosely typed language
4.2 declare variables
var statement is a standard variable declaration
Var variable declaration is permanent and can not be deleted with the delete operator
Global object, when the calling object is initialized, JS parser will look for variable var declared in the corresponding code segment, the
Then create the appropriate properties in the global object, calls it, in which case it is unassigned (undefined),
When the program executes the appropriate code segment when the declaration var give the corresponding object attribute assignment
Repeat statement: In accordance with the above understanding of the role of the var statement, repeated var statement will not cause any errors,
var statement only facilitate the creation of a global object, call the object's properties, but the code assigned by the
Missing statement: to undeclared variable assignment, JS implicitly declare global variables (increase the properties in the global object), and then assign it a value
4.3 Scope of variables
Global variables, local variables
When local and global variables of the same name, hides the global variable
4.3.1 no block-level scope
Variable function declared, wherever declared throughout the function they are all defined
4.3.2 undeclared variable and unassigned variables
alert (u); generates an error - the use of undeclared variables results in an error
var u; alert (u); pops undefined --- unassigned variables used, its default value used undefined
4.4 Basic types and reference types
Chapter III has spoken
4.5 Garbage Collection
With java
4.6 Variable as an attribute
4.6.1 Global Object
JS interpreter starts running, before any JS code, will create a global object, this property is the object of JS global variables,
And initialized to undefined
When JS var to declare a global variable, in fact, it is the definition of a property of the global object,
JS interpreter also many attributes with a predefined value and function to initialize global objects, such as: Infinity parseInt Math
Internal non-functions can use this to reference the global object
JS client's, Window object represents a browser window that contains the global object all JS code in the window, with a self-referential window property
4.6.2 Local variables: the calling object
Local variables are stored in the call object's properties
Call the object is a completely separate subject, it is possible to prevent global variable coverage of the same name
4.6.3 JS execution environment
When the JS interpreter to execute a function that creates an execution environment
Ifame case where the client JS: JS allows simultaneous multiple global execution environment, such as
4.7 depth understanding of variable scope
Each JS execution environment and has a scope chain associated with it which is a list of objects or object chain.
Queries x: process variables name resolution (variable name resolution), which began to view each object in the scope chain,
If so, return value, if there is no research to the next object to some analogy.
Priority scope chain: nested function call object> call object> global object
According to the above described process is understood initialization JS:
Before JS interpreter to execute any code, create a global object
A predefined value and to initialize the global object attributes, eg.Math, Infinity, parseInt
var statement outside the search function, create a global object corresponding properties, initialized to undefined
Create a global execution context, the scope chain is only one object - the global object
In order to execute code
Var statement encountered an assignment to the corresponding global object property assignment
Encounter undeclared assignment, a corresponding increase in properties in the global object and assign
Encounters a function call, the call object is created
The search function var statement and parameters to create the appropriate call object properties initialized to undefined
Create a function execution environment, the scope chain - the first object: the calling object; a second object: the global object
In order to execute code
Var statement encounters an assignment statement to call the object corresponding property assignment
Encounter undeclared assignment, a corresponding increase in properties in the global object and assign
Encounters a function call, the call object to create nested functions
Corresponding search nested function in var statements and parameters, create nested function calls the object properties initialized to undefined
Create a nested function execution environment, the scope chain - the first object: the object nested function calls; the second object: the calling object; third object: the global object
So on and so forth
eg1.
var scope="global";
function f(){
alert(scope);
var scope="local";
alert(scope);
}
f();
process:
Create global objects, var declaration outside the search function, create a scope attribute in the global object, scope = undefined
Create a global execution context, the scope chain is only one object: the global object
Followed by the implementation of code:
When var scope = "global", the variable name resolution to start, look for properties in the global scope object properties
The "global" assigned scope
Encounters a function call: the call object is created
The search function var declaration and parameters, create attributes scope of the call object, scope = undefined
Create a function execution environment, the scope chain: the calling object> Global Objects
Followed by the implementation of code:
alert (scope), query scope, the variable name resolution, first calling object search, find scope attribute, its value is undefined, execute
var scope = "local", the query scope, the variable name resolution, first calling object search, to find the scope attribute, scope = "local"
alert (scope), query scope, the variable name resolution, first calling object search, find scope attribute whose value is "local", execution
eg2.
var scope="global";
function f(){
alert(scope);
scope="local";
alert(scope);
}
f();
process:
Create global objects, var declaration outside the search function, create a scope attribute in the global object, scope = undefined
Create a global execution context, the scope chain is only one object: the global object
Followed by the implementation of code:
When var scope = "global", the variable name resolution to start, look for properties in the global scope object properties
The "global" assigned scope
Encounters a function call: the call object is created
The search function var declaration statements and arguments, no var declaration found
Create a function execution environment, the scope chain: the calling object> Global Objects
Followed by the implementation of code:
alert (scope), query scope, the variable name resolution, first calling object search, did not find the scope attribute, and then search the global object, find the scope attribute whose value is "global" execution
scope = "local", the query scope, the variable name resolution, first calling object search, did not find the scope Properties, and then search the global object, found scope attributes, scope = "local"
alert (scope), query scope, the variable name resolution, first calling object search, did not find the scope attribute, and then search the global object, find the scope attribute whose value is "local", execution
eg3.
scope1="global";
alert(scope1);
function f(){
alert(scope2);
scope2="local";
}
f();
process:
Create global objects, var declaration is not found, there is no global custom object properties
Create a global execution context, the scope chain is only one object: the global object
Followed by the implementation of code:
When scope1 = "global", the variable name resolution began, the scope chain is not found scope1 property, create scope1 properties in the global object properties, and assigned the "global"
When the alert (scope1), variable names begin parsing, the scope chain to find scope1 attribute whose value is "global", execution
Encounters a function call: the call object is created
The search function var declaration statements and arguments, no var declaration found
Create a function execution environment, the scope chain: the calling object> Global Objects
Followed by the implementation of code:
alert (scope2), query scope2, variable name resolution, the scope chain is not found scope2 property, an error scope2 is not defined
function
Extended look, function. Remember the pre-resolution mentioned above, in the pre-parsing javascript, in addition to the predefined variables var, also includes a function of extracting definitions, it is possible to define functions anywhere in the script at any local call. Any before it.
But the way defined functions, including the definition of a method call literals, declarations var function of the method. See below
alert (typeof y3); // result?
var y3 = function (){ console.log('1'); }
Remember this convention it: call must occur after the statement, and why, if you understand the above, in fact, here the answer has been clear. javascript engine in the pre-analytical var give them an initial value is undefined, this way, if we call it before it's declared, javascript engine not get its true value, it will naturally report "xxx is not a function" It faults. this is also why the same sort as a function declaration, but the relationship between a statement and call to order, but no such a constraint.
in conclusion
It is a function, is js execution, dynamic modification of the results, still follow the rules of the pre-analytical variables (above alert when it did not get the message literal function).
If the two mix it. See below, exist for y4 variables and function.
alert (typeof y4); // result?
function y4(){
console.log('y4')
}
where Y 4;
Statement high priority because javascript reason during the pre parsing function, it is natural for the y4 type function, but when the assignment y4 (js engine case in the process of execution), it js assignment will overwrite the function of statement. and so:
alert(typeof y5);
where Y 5 = 'Angle';
function y5(){
console.log('ghost');
}
alert(y5);
The first alert results, because it is in the process of top js execution, so to function. When the second time alert, its value has been rewritten to 5 (not to be defined function of the position of the lower confused.)
Js from parse and execute separately to think, only to find suddenly in front, the answer to many problems have surfaced are very natural, as the article the author says,
> "Once you understand the execution environment, the calling object, closures, lexical scoping, many of these phenomena scope chain concept, JS language can be solved."