The functions and circulation js

One.

<1. What is the function?

: Defined section of code, and can be used repeatedly block

<2 action functions:

Enhance code reusability will be predefined piece of code, used only when needed to trigger

Block: formation of a relatively independent scope

<3 syntax:

function function name (parameter list) {

Code block (function body) ==> block of code to be executed

return return value to return some result of the execution, the end of the function (after the program hit return, behind the return code will not be executed) can only have a return value

1. Scope function

Global function: some functions are generally written js independent function at any location in the outermost JS

Local function: the function of a function definition inside

2. The scope of variables

What is the scope?

: Life Cycle range of variables

JS in the scope of the classification:

<1. Function scope
of the variables are only accessible in the current scope

Only access to the current function, the function can not leave access

Var keyword variables defined in the body of the function is the local variables

<2. Global scope

Once defined, anywhere in the code can be accessed

Variable in the body of the function is not defined and assigned by the var keyword, it will become global variables

3. branch structure

Algorithms + data structures = programs

Any complex algorithm has the following structure:

<1, the sequence structure

<2, branch selection structure

<3, the cyclic structure

4. branch structure: meet different conditions to perform different programs

if a branch: when the conditions are met, running some statements may execute statement

if ... else ... branch: When the conditions are met, run some statement or else run the statement will perform a

Corresponding block of code when if () ... else if () .. else if () ... else satisfies a certain condition

grammar:

     if (condition) {

           Statement block

     }

    if (condition 1) {

          Statement block 1

    } Else if (condition 2) {

         Statement block 2

    } Else if (condition 3) {

        Statement block 3

    }...

    else{

       Statement block n

   }

<1, under conditions such that the boolean value or expression

<2, if the conditions are not boolean then automatically converted

The following situations will be converted to false by default

if(0){}

if(0.0){}

if (in) {}

if(undefined){}

if(""){}

if(null){}

if(false){}

Apart from the above cases, the other is converted to true

if(1){}

if(2){}

if ( "The weather today is good ah!") {}

<3, the latter (condition) if {} can be omitted, but is not recommended if omitted, then if only the structure under control of the first statement if

5.switch-case structure

    Action: In order to simplify determination conditions equivalent structure determination

   grammar:

   switch (variable / expression) {

   case value 1:

   Statement block 1;

   break; // end of the block of statements can be omitted

   case value of 2:

  Statement block 2;

  break;

  . .. ...

  default:

  N-statement block;

  break;

  }

II. Cycle

 Loop: repeat over and over again performing the same or similar code

Features:

1, cycling conditions: a predetermined number of execution cycles

2, the operation cycle: the same or displayed statement for execution

3, while the cyclic structure

while (condition) {

Loop

}

1) determining the loop condition

2) the loop condition is true, true, loop body is executed

Further determination condition, the condition is true if the loop body is executed

....

3) the loop condition is false, false, the loop operation ends

Cycle of three elements

<1, the loop variable

<2, when the execution condition is satisfied circulation loop

<3, the loop variable is the change in variation tends to condition is not satisfied, the end of the cycle tend to

4.For loop structure

grammar:

for (up to 1 represents; Expression 2; Expression 3) {

Loop

}

Expression 1: Declare variables can be omitted to declare good loop variable before the cycle begins

Expression 2: Analyzing cycling conditions may be omitted in the end of the loop to add Analyzing body of the loop will form an endless loop or

Expression 3: Update loop variable update statement can be omitted add loop variable in the loop body

Three expressions may be omitted, however; can not be omitted

Process:

1, the expression is executed declare the loop variable 1

2, determines the cycle set up condition is established is executed loop

3, executing the expression 3, updates the loop variable

2 expression execution cycle to determine whether the conditions set up set up the loop body is executed

2 expression is false result of the execution of the loop ends

Applies to: a fixed number of cycles

5.do while structure

grammar:

do{

Loop

} While (condition);

Process:

<1, first execution cycle thereof

<2, the loop condition is determined

If the loop condition holds true, the loop operation continues

If the condition is not satisfied lecture false, the loop exits

While the difference and do while

1, while: the first judgment is not executed after performing a loop may

2, do while: the first implementation, the determination is executed at least once loop

 

Guess you like

Origin www.cnblogs.com/hyh888/p/11261886.html
Recommended