Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Function

From MDC


Contents

Summary

Every function in JavaScript is actually a Function object.

Syntax

new Function ([arg1[, arg2[, ... argN]],] functionBody)

Parameters

arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "x", "theValue", or "a,b".
functionBody
A string containing the JavaScript statements comprising the function definition.

Description

Function objects created with the Function constructor are evaluated each time they are used. This is less efficient than declaring a function and calling it within your code, because declared functions are parsed only once.

All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.

Invoking the Function constructor as a function (without using the new operator) has the same effect as invoking it as a constructor.

Properties

For properties inherited by Function instances, see Properties of Function instances.

prototype
Allows the extension of all Function objects.

Properties inherited from Function.prototype
caller, constructor, length, name

Methods

For methods inherited by Function instances, see Methods of Function instances.

Although the Function object does not provide any methods of its own, it does inherit methods through the prototype chain.

Methods inherited from Function.prototype
apply, call, toSource, toString, valueOf

Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch

Function instances

Function objects inherit from Function.prototype. Modifications to the Function.prototype object are propagated to all Function instances.

Properties

arguments
Deprecated
An array corresponding to the arguments passed to a function. This is deprecated as property of Function, use the arguments object available within the function instead.
arity
Deprecated
Specifies the number of arguments expected by the function. Use the length property instead.
caller
Non-standard
Specifies the function that invoked the currently executing function.
constructor
Specifies the function that creates an object's prototype.
length
Specifies the number of arguments expected by the function.
name
Non-standard
The name of the function.

Methods

apply
Applies the method of another object in the context of a different object (the calling object); arguments can be passed as an Array object.
call
Calls (executes) a method of another object in the context of a different object (the calling object); arguments can be passed as they are.
toSource
Non-standard
Returns a string representing the source code of the function. Overrides the Object.toSource method.
toString
Returns a string representing the source code of the function. Overrides the Object.toString method.
valueOf
Returns a string representing the source code of the function. Overrides the Object.valueOf method.

Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch

Examples

Example: Specifying arguments with the Function constructor

The following code creates a Function object that takes two arguments.

var multiply = new Function("x", "y", "return x * y");
var theAnswer = multiply(7, 6);

The arguments "x" and "y" are formal argument names that are used in the function body, "return x * y".

See also