共用方式為


function Statement (Windows Scripting - JScript)

 

Declares a new function.

Syntax

function functionname([arg1 [, arg2 [,...[, argN]]]])
{
   statements
} 

Arguments

  • functionname
    Required. The name of the function.

  • arg1...argN
    Optional. An optional, comma-separated list of arguments the function understands.

  • statements
    Optional. One or more JScript statements.

Remarks

Use the function statement to declare a function for later use. The code that is contained in statements is not executed until the function is called from elsewhere in the script.

The return statement is used to return a value from the function. You do not have to use a return statement; the program will return when it reaches the end of the function. If no return statement is executed in the function, or if the return statement has no expression, the function returns the value undefined.

Note

When you call a function, be sure to always include the parentheses and any required arguments. Calling a function without parentheses causes the text of the function to be returned instead of the results of the function.

The following example illustrates the use of the function statement.

function myfunction(arg1, arg2)
{
    var r = arg1 * arg2;
    return(r);
}

A function can be assigned to a variable. This is illustrated in the following example.

function AddFive(x){    return x + 5;}function AddTen(x){    return x + 10;}var condition = false;var MyFunc;if (condition)    MyFunc = AddFive;else    MyFunc = AddTen;var result = MyFunc(123);// Output: 133

Requirements

Version 1

Change History

Date

History

Reason

September 2009

Added information to Remarks and added a second example.

Information enhancement.

See Also

new Operator (Windows Scripting - JScript)