JavaScript Advanced Programming (3rd Edition) Learning notes 5 JS Statements _ Basics

Source: Internet
Author: User
Tags case statement
Bricks and mortar, and then the wall, in the ECMAScript, the sentence is the walls we need to build. Statements and operators like, for people with a C background, it is natural, the following similar form to organize the relevant knowledge of the statement, highlighting some of the ECMAScript more special and personally think more interesting place, the same, no emphasis on the basis of the statement is not unimportant, But I think you are already familiar with.

Statement list

Statement Grammar Brief description
Simple statement

;

The statement ends with a semicolon (;), and can omit the semicolon without causing ambiguity.

Statement block

{}

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

If statement

if (condition) {}

if (ocndition) {}else{}

Conditional selection, which implicitly converts the result to a Boolean type in a conditional expression.

It is recommended that each branch explicitly use {} to avoid errors in maintenance.

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 consistent with the C language, but the expression in the switch is not limited to integral types.

1, in a switch statement, an expression is not limited to an integral type, it can be any expression.

2, in value after the case, can be an integral type, a different type, or even an expression, but no type conversion is performed when the comparison is made, or a match is made using congruent (= = =).

3, the break in the case branch indicates that the subsequent match is no longer continuing, and if omitted, the following case statement continues. It is recommended that you add a break to each case and, if you take advantage of this continuing performance, add the corresponding annotation.

4, the last branch of the break plus the same effect, my own personal style is added to maintain consistency.

Do-while statement

do{

Statement

}while (expression);

Execute the loop body first, then the conditional judgment, this format will perform at least one loop.

Conditional judgment also has an implicit conversion.

While statement

while (expression)

{

Statement

}

satisfies the condition before executing the loop body. If the condition is not met at first, the loop body is not executed at all.

For statement

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

Statement

}

The For statement is equivalent functionally and while.

The order of execution is to perform the initialization initialization first, then perform the condition comparison expression, if the condition is met, execute the loop body, perform a loop, execute the post-loop-expression section, and then cycle the comparison condition until the whole loop is out.

For-in statement

For (property in expression) {

Statement

}

Another form of a for loop that you can use to iterate through the properties of an object and the properties on the object's prototype chain.

With statement

With (expression) {

Statement

}

Sets the scope of the code to a specific object.

Label statement Label:statement; Add tags to your code for use by other statements.
Break statement

Break

Break label;

1, in the switch statement, after finding a matching case branch, do not continue to execute the following case statement.

2, use in the circular statement interrupt the whole loop.

Continue statement

Continue

Continue label;

Interrupts this loop in a circular statement to perform the next loop.

Try Statement

try{

}catch (e) {

}finally{

}

Place the code in a try block so that it can be handled appropriately when the exception occurs.

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

Return

return expression;

Returns the statement. Returns undefined after return is not returned.

For statements, the description is as follows:

1, about the statement to add a semicolon terminator (;), my point is to add to each statement, do not let the engine to guess your program. But yesterday I saw an article and my views on the contrary, but also quite sensible, although it did not change my point of view, but it also makes my horizons wider.

2, for the Var statement, due to the ECMAScript in the Declaration of Ascension Phenomenon, we recommend that a scope to use the variables are put to the top, with a var statement to define multiple variables, so easy to understand, and not easy to error. At present many JS libraries also use this form, the following is taken from the beginning of jquery code:

Copy Code code as follows:

var document = Window.document,
Navigator = Window.navigator,
location = window.location;

3, for the statement block ({}), can also be used to define the literal volume of objects. In ECMAScript, there is no block-level scope.
4, for four kinds of circular statements (Do-while, while, for, for-in), because the for-in statements each loop will search the object itself and its prototype, so the efficiency is relatively low. About optimization for A For Loop statement:
Copy Code code as follows:

1. General for Loop
for (Var i=0 i < arr.length; i++) {
}
2. Above in each cycle will recalculate the length of the arr, if the ARR is Dom operation, it will obviously affect the efficiency, you can improve
for (var i=0,l=arr.length; i<l; i++) {
}
3. So the whole cycle will only calculate one length, if you take into account the decline, can also be modified into
for (var i=arr.length; i>0; i--) {
}
4. The above does not use the intermediate variable and only need to calculate once the length, if takes into account the length is always a not less than 0 number, and in JS 0 Boolean value is false, can further modify to
for (var i=arr.length i; i--) {
}
5. Considering the possible impact of the variable declaration in JS, in order to eliminate the hidden danger, the modified
var i=arr.length;
for (; i; i--) {
}

5, for with statements, although sometimes fast, but also often lead to unpredictable results, recommended less, not even:
Copy Code code as follows:

1. Use with statement
With (obj) {
A=b;
}
2. Do not use the WITH statement, and 1 of the case is equivalent
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;

The 1th part uses the WITH statement, the 2nd part is the equivalent statement that does not use the WITH statement, the 3rd part is the final possible run result, if only from the WITH statement itself, it is not easy to understand what happens when the program actually runs. In addition, when using the With statement involves modification, there is a problem with the different steps, looking at the following code:
Copy Code code as follows:

var obj = {
person:{
Name: ' Linjisong '
}
};
With (Obj.person) {
Obj.person = {
Name: ' Oulinhai '
};
Console.info (Obj.person.name); Oulinhai
Console.info (name); Linjisong
}

There will inadvertently be a synchronization.
6, in return statement should be noted:
Copy Code code as follows:

Return
{
Prop: ' Value ';
}//because the engine will automatically add a semicolon, this will actually return undefined

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.