Availability: |
| ||||||||
JavaScript syntax: | - | myFunction = Function | |||||||
- | myFunction = new Function() | ||||||||
Object properties: | arity, caller, constructor, length, prototype | ||||||||
Object methods: | apply(), call(), toSource(), toString(), valueOf() | ||||||||
Collections: | arguments[] |
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:
Declared functions in script source text
Anonymous functions build with the Function object constructor
Implementation-supplied functions built into the host environment
Internal functions built into the language
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.
// Create a function object var myFunction = new Function("arg1, arg2", "return(arg1 * arg2);"); // And call it document.write(myFunction(5, 10));
Property | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
arity | 1.2 ![]() | 3.0 ![]() | 4.0 ![]() | 4.0 ![]() | ![]() | 3.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
caller | 1.1 ![]() | 1.0 ![]() | 3.0 ![]() | 3.02 ![]() | 3.0 ![]() | 2.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
constructor | 1.1 ![]() | 1.0 ![]() | 3.0 ![]() | 3.02 ![]() | ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | DontEnum. |
length | 1.1 ![]() | 1.0 ![]() | 4.0 ![]() | 3.02 ![]() | ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | ![]() |
prototype | 1.1 ![]() | 1.0 ![]() | 3.0 ![]() | 3.02 ![]() | 3.0 ![]() | 2.0 ![]() | 2 ![]() | ![]() | ![]() | ![]() | ReadOnly, DontDelete, DontEnum. |
Method | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
apply() | 1.3 ![]() | 5.5 ![]() | 4.06 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
call() | 1.3 ![]() | 5.5 ![]() | 4.06 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
toSource() | 1.3 ![]() | 3.0 ![]() | 4.06 ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | - |
toString() | 1.1 ![]() | 3.0 ![]() | 4.0 ![]() | 3.0 ![]() | 3.0 ![]() | 2.0 ![]() | 2 ![]() | ![]() | ![]() | ![]() | - |
valueOf() | 1.1 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | - |
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
Prev | Home | Next |
Function literal | Up | Function() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |