JavaScript advanced programming (version 3rd) study notes 5 js statements

Source: Internet
Author: User
Tags case statement

The bricks and tiles and cement are available, and the next step is to build the wall. In ECMAScript, the statement is the wall we need to build. The statement is the same as the operator. For people with a C background, it is no longer necessary. The following describes the statements in a similar way, highlight some of the highlights of ECMAScript that are particularly interesting to individuals. Similarly, the basic statements that are not emphasized are not unimportant, but I think you are familiar with them.

Statement Overview

Statement Syntax Brief Description
Simple statement

;

The statement ends with a semicolon (;). The semicolon can also be omitted without ambiguity.

Statement Block

{}

Use braces ({}) to put a group of statements together to form a statement block. In ECMAScript, there is a statement block, but there is no statement block scope.

If statement

If (condition ){}

If (ocndition) {} else {}

Select a condition. In a condition expression, the result is implicitly converted to the Boolean type.

We recommend that you use {} for each branch to avoid maintenance errors.

Conditional statements can be nested.

Switch statement

Switch (expression)

{

Case value1:

Statement1;

Break;

Case value2:

Statement2;

Break;

Default:

Statement;

Break;

}

The switch statement syntax is the same as that in the C language. The difference is that the expression in the switch is not limited to integer.

1. In a switch statement, an expression is not limited to an integer and can be any expression.

2. In the value after case, it can be an integer, another type, or even an expression, but no type conversion is performed during comparison, that is, use full equality (=) for matching.

3. the break in the case branch indicates that the subsequent matching will not be continued. If it is omitted, the following case statement will continue to be executed. We recommend that you add a break to each case. If you use this feature to continue execution, add the corresponding annotations.

4. The break and effect of the last branch are the same. My own personal style is to maintain consistency.

Do-while statement

Do {

Statement;

} While (expression );

Execute the loop body first and then perform condition judgment. This format will execute at least one loop.

Conditional judgment can also be implicitly converted.

While statement

While (expression)

{

Statement;

}

The loop body is executed only when the conditions are met. If the conditions are not met at the beginning, the loop body will not be executed at all.

For statement

For (initialization; expression; post-loop-expression ){

Statement;

}

The for statement is functionally equivalent to the while statement.

The execution order is: Initialize initialization first, and then compare expression with the condition. If the condition is met, execute the loop body. After a loop is executed, execute the post-loop-expression part, then compare the conditions until the entire cycle exists.

For-in statement

For (property in expression ){

Statement;

}

Another form of for loop can be used to traverse the attributes of an object and the attributes on the prototype chain.

With statement

With (expression ){

Statement;

}

Set the code scope to a specific object.

Label statement Label: statement; Add tags to the Code for other statements.
Break statement

Break;

Break label;

1. Used in the switch statement. After a matched case Branch is found, the following case statement is not executed.

2. Interrupt the entire loop in a loop statement.

Continue statement

Continue;

Continue label;

Interrupt this loop in the loop statement and execute the next loop.

Try statement

Try {

} Catch (e ){

} Finally {

}

Place the code in the try block so that the exception can be handled accordingly.

Throw statement Throw e; Throw an exception.
Debugger statement Debugger; Debugging.
Return Statement

Return;

Return expression;

Return Statement. When no return is returned after return, undefined is returned.

The statements are described as follows:

1. Do you want to add a semicolon terminator (;) to a statement? In my opinion, add each statement. Do not let the engine guess your program. However, I saw an article yesterday, which is the opposite of my point of view. It is quite reasonable. Although it has not changed my point of view, it has broadened my horizons.

2. For var statements, because ECMAScript has a declarative elevation phenomenon, we recommend that you put all the variables used in a scope on the top and define multiple variables with a var statement, which is easy to understand, it is not easy to make mistakes. Currently, many JS libraries use this form. The following code is taken from the beginning of jQuery:

Copy codeCode: var document = Invalid invalid Doc ument,
Navigator = window. navigator,
Location = window. location;

3. The block ({}) can be used to define the object literal volume. In ECMAScript, there is no block-level scope.
4. for the four loop statements (do-while, while, for, for-in), because each loop of the for-in statement searches for the object itself and its prototype, therefore, the efficiency is relatively low. Optimization of for loop statements:Copy codeThe Code is as follows: // 1. General for Loop
For (var I = 0; I <arr. length; I ++ ){
}
// 2. The length of arr will be re-calculated in each loop above. If arr is a dom operation, the efficiency will be significantly affected. You can improve the efficiency.
For (var I = 0, l = arr. length; I <l; I ++ ){
}
// 3. In this way, the length of the entire loop is calculated only once. You can change it
For (var I = arr. length; I> 0; I --){
}
// 4. the length of the intermediate variable is not used and only needs to be calculated once. If the length is always a number not less than 0 and the Boolean value of 0 in JS is false, you can change it
For (var I = arr. length; I --){
}
// 5. Considering the possible impact of variable declaration elevation in JS, modify it to eliminate potential risks.
Var I = arr. length;
For (; I --){
}

5. For with statements, although they are sometimes quick, they often lead to unexpected results. We recommend that you use them less or even not:Copy codeThe Code is as follows: // 1. Use the with statement
With (obj ){
A = B;
}
// 2. Do not use the with Statement, which is equivalent to 1.
If (obj. a === undefined ){
A = obj. B | B;
} Else
{
Obj. a = obj. B | B;
}
// 3. Possible results
A = B;
A = obj. B;
Obj. a = B;
Obj. a = obj. B;

Part 1 is the with statement, Part 2 is the equivalent statement that does not use the with statement, and part 3 is the final possible running result, it is not easy to understand what will happen when the program is actually running. In addition, when the with statement is used for modification, there will be non-synchronization issues. refer to the following code:Copy codeThe Code is as follows: var obj = {
Person :{
Name: 'linjisong'
}
};
With (obj. person ){
Obj. person = {
Name: 'external'
};
Console.info (obj. person. name); // oulinhai
Console.info (name); // linjisong
}

In this case, a non-synchronization occurs inadvertently.
6. Note the following when returning a return statement:Copy codeThe Code is as follows: return
{
Prop: 'value ';
} // Because the engine automatically adds a semicolon, undefined is actually returned here

Return {
Prop: 'value ';
} // Returns an object

Related Article

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.