Function object (Object/core)

An object of the class "Function".


ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
JavaScript syntax:-myFunction = Function
-myFunction = new Function()
Object properties:arity, caller, constructor, length, prototype
Object methods:apply(), call(), toSource(), toString(), valueOf()

An instance of the class "Function" is created by using the new operator on the Function() constructor. The new object adopts the behavior of the built-in prototype object through the prototype-inheritance mechanisms.

All properties and functions of the prototype are available as if they were part of the instance.

Many built-in objects are functions. They can be invoked with arguments. Some of these are constructors. They are functions that are intended to be used with the new operator.

Function objects come in four varieties according to how they are implemented. They may be built-in to the interpreter or may be provided as extensions in the script itself. The four types of functions are:

JavaScript has such relaxed syntax rules that it forgives the programmer if the arguments to a function are omitted. Instead, the interpreter will automatically pass the undefined value in place of the missing argument.

Every built-in function has the Function prototype object as the value returned by its internal Prototype property with the exception of the Function prototype object itself, which would return the Object prototype object.

The prototype for the Function prototype object is the Object prototype object.

If you want to create a function with no name, you can create your own Function object with the new operator. That function would have some script source associated with it and you can then call your function directly. Although it won't have a name, it would appear as if it did in the script source when you call it. However the name it would appear to have is actually the name of the variable containing the reference to it. Since it is an object, two variables can refer to the same object and you could call the same function under two different names. That might happen if you pass the function as an argument in the calling interface to another function. This is a way of implementing call-backs. You might build a comparator like this and pass it to a sort() function.

Creating function objects and referring to them in variables is somewhat like using an eval() function to execute script source that you have built in a string.

Example code:

   // Create a function object
   var myFunction = new Function("arg1, arg2", "return(arg1 * arg2);");
   // And call it
   document.write(myFunction(5, 10));

See also:Aggregate type, Anonymous function, Arguments.callee, Arguments.caller, Built-in function, Declared function, Execution context, Function call, function( ... ) ..., Function(), Function.arity, Function.Class, Function.length, Function.prototype, Implementation-supplied function, Internal function, JavaScript to Java values, Native object, Object object, Parameter

arity1.2 1.23.0 3.04.0 4.04.0 4.0 n/a3.0 3.0 n/a n/a n/a n/aWarning , ReadOnly, DontDelete, DontEnum.
caller1.1 1.11.0 1.03.0 3.03.02 3.023.0 3.02.0 2.0 n/a n/a n/a n/aWarning , ReadOnly, DontEnum., Deprecated
constructor1.1 1.11.0 1.03.0 3.03.02 3.02 n/a n/a2 2 n/a n/a n/aDontEnum.
length1.1 1.11.0 1.04.0 4.03.02 3.02 n/a n/a2 2 n/a n/a n/aWarning , ReadOnly, DontDelete, DontEnum., Deprecated
prototype1.1 1.11.0 1.03.0 3.03.02 3.023.0 3.02.0 2.02 2 n/a n/a n/aReadOnly, DontDelete, DontEnum.

apply()1.3 1.35.5 5.54.06 4.065.5 5.5 n/a n/a3 3 n/a n/a n/a-
call()1.3 1.35.5 5.54.06 4.065.5 5.5 n/a n/a3 3 n/a n/a n/a-
toSource()1.3 1.33.0 3.04.06 4.064.0 4.0 n/a n/a n/a n/a n/a n/a-
toString()1.1 1.13.0 3.04.0 4.03.0 3.03.0 3.02.0 2.02 2 n/a n/a n/a-
valueOf()1.1 1.1 n/a4.0 4.0 n/a n/a n/a2 2 n/a n/a n/a-


ECMA 262 edition 2 - section - 10.1.1

ECMA 262 edition 2 - section - 10.2.4

ECMA 262 edition 2 - section - 13

ECMA 262 edition 2 - section - 15

ECMA 262 edition 2 - section - 15.3

ECMA 262 edition 3 - section - 10.1.1

ECMA 262 edition 3 - section - 13

ECMA 262 edition 3 - section - 15.3

O'Reilly JavaScript Definitive Guide - page - 42