Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Function Object

Creates a new function.

function Function( [[param1 : String, [..., paramN : String,]] body : String ])
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.

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.

NoteNote

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.

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.