Udostępnij za pośrednictwem


Function Object

Creates a new function.

function Function( [[param1 : String, [..., paramN : String,]] body : String ])

Arguments

  • param1, ..., paramN
    Optional. The parameters of the function. Each parameter may have type annotation. The last parameter may be a parameterarray, which is denoted by three periods (...) followed by a parameter array name and a typed array type annotation.

  • body
    Optional. A string that contains the block of JScript code to be executed when the function is called.

Remarks

The Function constructor allows a script to create functions at run time. The parameters passed to the Function constructor (all but the last parameter) are used as the parameters of the new function. The last parameter passed to the constructor is interpreted as the code for the body of the function.

JScript compiles the object created by the Function constructor at the time the constructor is called. Although this allows your script to have great flexibility in redefining functions at run time, it is also makes the code much slower. Use the Function constructor as little as possible to avoid slow scripts.

When calling a function to evaluate, always include the parentheses and required arguments. Calling a function without parentheses returns the Function object for that function. The text of a function can be obtained by using the toString method of the Function object.

Note

Only JScript provides the Function object. Since it is not derived from a .NET Framework type, other Common Language Specification (CLS) languages cannot use it. Consequently, when type-annotating the parameters and return types of CLS-compliant methods, make sure to use the System.EventHandler data type instead of the Function object. However, you may use the Function object to type annotate identifiers other than parameters or return types. For more information, see Writing CLS-Compliant Code.

Example

The following example illustrates a use of the Function object.

var add : Function = new Function("x", "y", "return(x+y)");
print(add(2, 3));

This code outputs:

5

Properties and Methods

Function Object Properties and Methods

Requirements

Version 2

See Also

Reference

function Statement

new Operator

var Statement