August 2 = 321-330

Source: Internet
Author: User

13.7.6 break and continue

Both Break and continue can be used to terminate a loop. The difference is that continue only terminates this loop and starts the next loop.

Break completely terminates the entire loop and starts executing the code after the loop.

Break code example:

For (VAR I = 0; I <5; I ++)

{

// Once I is output, the loop ends.

Document. Write (I );

Break;

}

Continue code example:

For (VAR I = 0; I <5; I ++)

{

// Skip the I = 1 loop and execute the next loop

If (I = 1)

{

Continue;

}

Document. writeln (I );

}

 

13.8 Functions

 

13.8.1 define the method in function 3

Javascript currently supports the function definition method in 3.

Define the name function:

Function Hello (name)

{

Alert (name + "hello ");

}

Define anonymous Functions

VaR F = function (name)

{

Alert (name + "hello ");

}

F ('zhang san ');

Use Function-like Anonymous Functions

Function can accept a series of string parameters. The last string parameter is the execution body of the function. The statements of the execution body are separated by;, while

The preceding string parameters are function parameters.

Code example:

VaR F = new function ('name', "document. writeln (' '+ name );");

F ('zhang san ');

 

13.8.2 recursive functions

A recursive function is a special function that allows you to call the function itself in the function definition.

Code example:

VaR factorial = function (N)

{

// Determine whether the data type is Numeric

If (typeof (n) = "Number ")

{

// If n is equal to 1, 1 is directly returned.

If (n = 1)

{

Return 1;

}

Else

{

// Return values recursively when n is not equal to 1

Return N * (factorial (n-1 ));

}

}

Else

{

Alert ("the parameter type is incorrect! ");

}

}

Alert (factorial (5 ));

 

13.8.3 local functions

We have introduced the concept of local variables. The variables defined in a function are called local variables, and functions defined in the same function are also called local functions.

Code example:

// Define global functions

Function outer ()

{

Function inner1 ()

{

Document. Write ("partial function 11111 <br/> ");

}

Function inner2 ()

{

Document. Write ("partial function 22222 <br/> ");

}

Document. Write ("start to test the local function... <br/> ");

Inner1 ();

Inner2 ();

Document. Write ("End test partial function... <br/> ");

}

// Call global functions

Outer ();

 

13.8.4 functions, methods, objects, and classes

After defining a function using JavaScript, you can get the following four items.

Function: Like a Java method, this function can be called.

Object: when defining a function, the system also creates an object, which is an instance of the function class.

Method: when defining a function, the function is usually attached to an object as the method of the object.

Class: when defining a function, a class with the same name as the function is also obtained.

        

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.