JavaScript Advanced Programming (3) Basic concepts

Source: Internet
Author: User

Syntax: You typically use one or more keywords to accomplish a given task.

If statement:

The condition can be an arbitrary expression, and the result of evaluating the expression is not necessarily a Boolean value. ECMAScript automatically calls the Boolean () conversion function to convert the result of this expression to a Boolean value. If the evaluation result is true, statement 1 is executed and statement 2 is executed if the evaluation result is false. These two statements can be either a single line of code or a block of code (a pair of multiple lines of code enclosed in curly braces).

if (i >) {    alert ("great than.");              // single-line statement  Else{  alert ("less than or equal to");     // the statement in the code block }

Always use code blocks, even if there is only one line of code.

Do-while statement:

Post-Test loop statement: The code in the loop body is executed at least once before the expression is evaluated.

var i = 0;  Do {    + = 2;}  while (I <); alert (i);

While statement:

Pre-Test Loop statement: The code in the loop body may never be executed.

var i = 0;  while (I <) {    + = 2;     } alert (i);

For statement:

Pre-Test loop statement: But it has the ability to execute the loop before initializing the variable and defining the code to execute after the loop. It is also possible that the code in the loop body will not be executed.

var count = ten; // var i;  for (var i = 0; i < count; i++) // loop variable initialization statement; loop condition; Change the loop variable's statement {    alert (i); // Loop Body  //ten

A loop variable initialization statement can be omitted, or multiple values          can be initialized; Initialize the variables used in the loop
The cyclic condition can be omitted, but a break must be provided within the loop; Criteria for evaluating initial variables
Statements that change the loop variable can be omitted, and the increment can be negative or larger to increase the value of the initial variable.
If all is omitted, an infinite loop is created.

The use of a For loop does not work with a while loop.

The variable initialization statement for the For loop, which can be executed externally without using the var keyword.

Even if I is a variable defined inside the loop, it can be accessed outside of the loop.

For-in statement:

A precise iteration statement that can be used to enumerate the properties of an object.

 for (var in window) {    document.write (propname);}

in this example, the for-in loop is used to display all the properties of the Window object in the BOM. Each time a loop is executed, a property name that exists in the Window object is assigned to the variable propname. This process persists until all properties in the object are enumerated again.

The properties of the ECMAScript object are not ordered, and the order of the property names that are output through the for-in loop is unpredictable. In particular, all properties are returned once, but the order in which they are returned may be different from the browser.

If the variable value of the iterated object is null or the Undefined,for-in statement throws an error. The loop body is no longer executed.

Label statement:

You can add tags to your code for future use.

Start:for (var i = 0; i < count; i++) {  alert (i);}

The start tag defined in this example can be applied in the future by a break or continue statement. Tagged statements are generally used in conjunction with loop statements such as for statements.

Break and Coutinue statements : Used to precisely control the execution of code in a loop.

The break statement exits the loop immediately, executing the statement following the loop;

The continue statement also exits the loop immediately, but exits the loop and resumes execution from the top of the loop;

varnum = 0;varnum = 0;  for(varI=1; i<10; i++) for(varI=1; i<10; i++)            {                                              {    if(i% 5 = = 0)if(i% 5 = = 0)    {                                               {         Break;Continue; }} num++; num++;}    }alert (num); /*4*/alert (num);//8

Both the break and continue statements can be used in conjunction with a label statement to return a specific location in the code. This combination of usage occurs in cases where the loop is nested.

varnum = 0;varnum = 0; Outermost:outermost: for(vari=0; i<10; i++) for(vari=0; i<10; i++){                                                       {     for(varj=0; j<10; J + +) for(varj=0; j<10; J + +)           {                                                      {        if(i==5 && J ==5)if(I==5 && J ==5)        {                                                      {               Breakoutermost;Continueoutermost; }} num+ + num++           }                                                       }} }alert (num);/* -*/alert (num);// the

It is recommended that if you use a label statement, you must use a descriptive label while not nesting too many loops.

With statement: Read to understand, not recommended to use not detailed.

Switch statement:

The Swith statement is most closely related to the IF statement and is also a flow control statement commonly used in other languages.

Switch(expression) Casevalue:statement;  Break;  Casevalue:statement;  Break;  Casevalue:statement;  Break;  Casevalue:statement;  Break;...   Casevalue:statement;  Break; default: statement;

Each case is a representation of "execute statementif expression equals value".

The keyword break causes the code to jump out of the switch statement. If there is no keyword break, the code execution continues into the next case.

The keyword default describes an action when the result of an expression is not equal to any one case (in fact, it is relative to the Else statement).

You can use any data type in a switch statement, both strings and objects. The value of each case is not necessarily a constant, it can be a variable. Even an expression.

The switch statement uses the strict equality operator when comparing values, so type conversions do not occur. (The string "10" is not equal to the number 10).

Function:

A function can encapsulate any number of statements, and it can be invoked at any time anywhere. Functions in ECMAScript are declared by using the function keyword followed by a set of parameters and the body of the function.

Functions can be called by their function names, followed by a pair of parentheses and arguments (if there are more than one argument in parentheses, a comma can be separated).

Any function can implement the return value at any time by the return statement followed by the value to be returned. The function stops and exits immediately after executing the return statement.

A function that does not specify a return value returns a special undefined value.

Either the function always returns a value or never returns a value. Otherwise, the function sometimes returns a value and sometimes does not return a value, which can be inconvenient for debugging code.

Parameters:

Ecmascipt doesn't mind passing in the number of arguments, and doesn't care what data type the parameter is. These parameters can also be accessed through the arguments object.

Named parameters that do not pass a value are automatically assigned the undefined value.

All parameters in the ECMAScript are passed as values, and arguments cannot be passed by reference.

No overloads: The ECMAScript function cannot be overloaded because there are no attributes for function signatures.

JavaScript Advanced Programming (3) Basic concepts

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.