Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Function Object (JavaScript)

Creates a new function.

function functionName([argname1 [, ...[, argnameN]]])
{
   body
}
functionName = new Function( [argname1, [... argnameN,]] body );
functionName

Required. The name of the newly created function

argname1...argnameN

Optional. A list of arguments the function accepts.

body

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

The function is a basic data type in JavaScript. Syntax 1 creates a function value that JavaScript converts into a Function object when necessary. JavaScript converts Function objects created by Syntax 2 into function values at the time the function is called.

Syntax 1 is the standard way to create new functions in JavaScript. Syntax 2 is an alternative form used to create function objects explicitly.

For example, to create a function that adds the two arguments passed to it, you can do it in either of two ways:

function add(x, y)
{
   return(x + y);            //Perform addition and return results.
}
var add = new Function("x", "y", "return(x+y)");

In either case, you call the function with a line of code similar to the following:

add(2, 3);
NoteNote

When calling a function, ensure that you 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.

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards. See Version Information.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.