JavaScript scopes and variable objects, javascript Variables
Variable object
Let's talk about what is a variable object and what is stored in the variable object.
The execution environment in JavaScript includes the Global execution environment and the function execution environment. Every time you enter an execution environment, a variable object is created, this object records the variables that can be accessed in the current execution environment. They exist as attributes of the variable object. That is to say, this variable object becomes the entity of the abstract concept of "Scope.
At the same time, the attribute records in the variable object are ordered in a certain order, and whether the attribute value is an actual value or undefined is also in stages (entering the context (the function starts to call, but the specific internal code has not been executed); The Execution Code stage ).
1. In the global environment:
(1) function declaration
(2) The variable Declaration-its value is undefined and is assigned a specific value until the statement is executed.
2. In the function environment:
When you enter the execution environment, the variable object will be initialized as follows:
(1) arguments object. The value in the object is assigned with a specific real parameter value.
(2) function parameters: an attribute of the variable object. Its attribute name is the name of the parameter, and its value is the value of the real parameter. For parameters not passed, its value is undefined.
(3) function declaration: an attribute of a variable object. Its Attribute names and values are created by the function object, and its values are references to a function object;
(4) variable Declaration: an attribute of a variable object. Its attribute name is the variable name and its value is undefined.
When executing a specific statement inside the function, the variable whose value is undefined described above will be assigned a specific value.
To sum up one sentence: when entering the context (such as entering a global environment or calling a function), the variable object except arguments, the function declaration, and the parameter is assigned a specific attribute value, the default attributes of other variables are undefined.
Statement escalation
**
JavaScript has the "Declaration elevation" feature. Declarations using function or var are promoted to the top of the current scope.
**
In fact, we can see what happened after entering an execution environment. As mentioned above, after entering a context, the declared function (the value is the reference of the function object) and variables (whose value is undefined) are recorded in the variable object. When the code execution stage is reached, search for the identifier in the scope chain and execute a statement to replace the variable that originally entered the context stage with undefined with a specific value.
Function declaration escalation
Take a look at Article (3). When entering the execution environment, the function declaration will be added to the variable object. Its corresponding attribute value is a reference to the declared function object, if a function call statement exists before a function declaration statement, the engine searches for the identifier in the scope and finds the declared function name in the variable object, the value also exists, that is, the reference to the created function object. Therefore, no error is reported when writing the code. The function is successfully called.
Instead of starting with a function, a function is assigned to a variable, in the context stage, no specific value is assigned to the variable (see article (4). Therefore, the variable value is undefined at this time, and it will wait until the code stage is executed, after this statement is executed, the value will be changed to a reference pointing to the function object. Therefore, if the statement that calls the function is in the front, an error will be reported. In this case, search for the identifier in the variable object, its value is still undefined. Of course it cannot be called successfully.
Let's take a look at an example of variable declaration elevation.
var a = "Hello";function b() { alert(a); //undefined var a = "World"; alert(a); //World}b();
Why is the first alert (a) undefined when B () is executed? To see what happened in the global execution environment, the variable object in the global environment is filled:
VO = {
B: Reference to a function object,
A: undefined-> after the sentence is executed, it becomes "hello"
}
Let's take a look at what happened to call B (), and its variable object is filled:
VO = {
Arguments :{},
A: undefined --> it is assigned "world" only after a specific statement is executed"
}
Note that the first execution of alert (a) will search for identifier a in the current scope and find that there is already a (then it will no longer search for it along the scope chain to the outer layer ), the value is undefined. When var a = "World" is executed, it is replaced with a specific value.
If var is removed from B, a variable a in the global environment will be searched along the scope chain, then both alert (a) will output "hello ".
References: javascript execution environment, variable object, and scope chain
Improved scope and variable declaration in JavaScript
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.