Easily overlooked JS script features

Source: Internet
Author: User

1. Ignored local variables
Copy codeCode: var a = 5;
(Function (){
Alert ();
Var a = a ++;
Alert ();
})()
Alert ();

Think about the execution result of this Code.
After the command is executed, check whether it is consistent with what you think?
OK. The core knowledge in this code is var a = a ++. Both of the variables a are the local variables in the anonymous function. They are the same and different from the global variable.
Why? Let's take a look at the definition of the variable declaration statement in the ECMA specification:Copy codeThe Code is as follows: Description
If the variable statement occurs inside a FunctionDeclaration,
Variables are defined with function-local scope in that function,
Described in s10.1.3. Otherwise, they are defined with global scope
(That is, they are created as members of the global object, as described
In 10.1.3) using property attributes {DontDelete}. Variables are
Created when the execution scope is entered. A Block does not define a new
Execution scope. Only Program and FunctionDeclaration produce a new
Scope. Variables are initialised to undefined when created. A variable
An Initialiser is assigned the value of its AssignmentExpression when
VariableStatement is executed, not when the variable is created.

As mentioned in the declaration: after entering the scope environment, the variable will be created and assigned the initial value undefined. When the variable declaration statement is executed, the value of the value assignment expression is assigned to the variable, not when the variable is created.
Therefore, the above Code can be equivalent:Copy codeThe Code is as follows: var;
A = 5;
(Function (){
Var;
Alert ();
A = a ++;
Alert ();
})()
Alert ();

This should be easier to understand.
2. Easy to ignore global variables
Copy codeThe Code is as follows: (function (){
Var a = B = 5;
})()
Alert (B );

This is the knowledge point YU Bo shared a few days ago. It makes sense and I will also analyze it here.
First, consider why the execution result is: 5.
OK. The reason is var a = B = 5.
To thoroughly analyze this statement, we will continue to define the statement with reference to the ECMA specification:
Var a = B = 5; the same as var a; a = B = 5; the two statements, the latter, are the value assignment expressions, and their definitions in ECMA are as follows:Copy codeThe Code is as follows: Simple Assignment (=)
The production AssignmentExpression: LeftHandSideExpression =
AssignmentExpression is evaluated as follows:
1. Evaluate LeftHandSideExpression.
2. Evaluate AssignmentExpression.
3. Call GetValue (Result (2 )).
4. Call PutValue (Result (1), Result (3 )).
5. Return Result (3 ).

For expression a = B = 5; first, execute expression a on the left. This is an identifier expression. According to the specification, the execution method is as follows:Copy codeThe Code is as follows: During execution, the syntactic production PrimaryExpression: Identifier
Is evaluated using the following algorithm:
1. Get the next object in the scope chain. If there isn't one, go to step 5.
2. Call the [[HasProperty] method of Result (1), passing the Identifier
The property.
3. If Result (2) is true, return a value of type Reference whose base
Object is Result (1) and whose property name is the Identifier.
4. Go to step 1.
5. Return a value of type Reference whose base object is null and whose
Property name is the Identifier.

Search for the scope chain and find the reference of the nearest a. Obviously, it can be found within the scope of the anonymous function, so variable a is determined.
Then execute the expression B = 5 on the right, or a value assignment expression. Repeat the first step of the assignment rule because variable B has not been declared in the anonymous function environment, so go to the window global environment and find the window. b. It is implicitly declared as a global variable and assigned to 5. According to the fifth step of the rule, the expression result is also assigned to. The final result is 5 for both a and B. The difference is that a is a local variable while B is a global variable.
Let's take another look at the overall execution sequence in the (function () {var a = B = 5}) () expression:
1. Create variable a in the anonymous function;
2. Assign the initial value undefined;
3. Get the reference of variable a; //
4. Get the reference of variable B; // window. B
5. Evaluate the number 5;
6. assign a value of 5 to B's reference: window. B;
7. Return result 5 of Result B = 5 to reference a:;
8. Return result 5 with a = 5;
Obviously, a step in the middle makes variable B declared as a global variable. After understanding it, we can easily find the optimization point of the Code: simply explicitly declare variable B as a local variable:Copy codeThe Code is as follows: (function (){
Var a, B;
A = B = 5;
})()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.