Function object
Represents a function in JavaScript.
Functions are reusable code snippets that can be used as many times as you wish, like in any other language.
The Function object inherits from the Function.prototype object, and the Function.prototype object inherits from the Object.prototype object. The Object.prototype object is the base object for all JavaScript objects. The Function object inherits all of its properties and methods from the Function.prototype and the Object.prototype objects, except the prototype property that refers to the Function.prototype object. Objects that can be instantiated inherit from the Function.prototype, the others inherit from the Object.prototype object.
The Function object inherits from the Function.prototype object, and the Function.prototype object inherits from the Object.prototype object. The Object.prototype object is the base object for all JavaScript objects. The Function object inherits all of its properties and methods from the Function.prototype and the Object.prototype objects, except the prototype property that refers to the Function.prototype object. Objects that can be instantiated inherit from the Function.prototype, the others inherit from the Object.prototype object.
Syntax:
Creating a Function object:
Creating a Function object with function literal:
var funcName = new Function ([arg1[, arg2[, ... argN]],] functionBody)
arg1[, arg2[, ... argN] | Optional. Strings that specifies the name of the parameters. |
functionBody | Optional. String that specifies the code that will be executed when the function is called. If any string value is specified in the constructor of the Function object, the last string value is always the functionBody parameter. |
function funcName ([arg1[, arg2[, ... argN]]) {
functionBody
}
or
var funcName = function ([arg1[, arg2[, ... argN]]) {
functionBody
}
functionBody
}
or
var funcName = function ([arg1[, arg2[, ... argN]]) {
functionBody
}
arg1[, arg2[, ... argN] | Optional. The parameters of the function. |
functionBody | Optional. Block of code that will be executed when the function is called. |
Function operations
Creating a simple function:
The Multiply function multiplies two values.
The Multiply function in this example is equivalent to the previous one.
How to call a function:
Recursive functions:
The Factorial function returns the factorial of an integer.
A function with a variable-length argument list:
The Add method uses the arguments object that contains information about the parameters specified by the caller.
Nested functions:
Nested functions are only exists within the scope of a specific function, they cannot be called directly from outside.
Functions are also objects:
Creating member functions:
Not recommended solution:
Recommended solution:
In case of the first solution, each instance of the Rectangle object has its own copy of the Area method.
It is inefficient, because extra memory is allocated for each instance.
The second solution is better. In that case, the Area method exists in only one instance and it is shared amongst all the instances of the Rectangle object. |
Members:
The Function object inherited from the Function.prototype and Object.prototype objects.
The following lists only contain the members of the Function and Function.prototype objects.
(*) - The property is inherited from the Function.prototype.
(*) - The method is inherited from the Function.prototype.
Properties:
Property | Support | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
arguments* | Deprecated, use the global arguments object instead. Refers to an object that contains information about the parameters specified by the caller. The number of the parameters specified by the caller can be different from the number of parameters declared for the current function (length property). With the use of the arguments object, functions with variable-length parameter list can be implemented. See the page for the arguments object and Example 1 and 2 below for further details. | |||||||||||
arity* | Returns the number of parameters declared for the current function. Deprecated, use the cross-browser length property instead. | |||||||||||
caller* | Returns the function that executed the current function, if any. | |||||||||||
constructor* | Returns a reference to the constructor function that created the current object. | |||||||||||
length* | Returns the number of parameters declared for the current function. If you need the number of the parameters specified by the caller, use the arguments object. See Example 1 for further details. | |||||||||||
name* |
|
Returns the name of the current function. | ||||||||||
prototype | Returns a reference to the Function.prototype object. The Function.prototype object allows adding properties and methods to the Function object that can be used with instances of the Function object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the Function object, only Function.prototype is allowed. |
(*) - The property is inherited from the Function.prototype.
Methods:
Method | Support | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
apply ([thisObj [,argArray]])* |
Executes a method of an object in the context of another object.
Typically used for variable-length argument lists and object inheritance.
The apply and call methods are similar, the only difference is that the apply method passes the parameters as an array.
See Example 3, 4, and 5 for further details.
|
||||||||||
call (thisObj [, arg1 [, arg2 [, ...]]])* |
Executes a method of an object in the context of another object.
Typically used for object inheritance.
The apply and call methods are similar, the only difference is that the apply method passes the parameters as an array.
See Example 3, 4, and 5 for further details.
|
||||||||||
toSource ( )* | Returns a string representing the source code of the current function. | ||||||||||
toString ( )* | Returns a string representing the value of the current function. When a function needs to be converted to a string, the JavaScript interpreter automatically calls its toString method. |
(*) - The method is inherited from the Function.prototype.
Examples:
Example 1:
A complete argument test.
Example 2:
This example shows how to implement a function that receives a variable-length argument list and returns their sum.
Example 3:
This example shows how to use the apply method for variable-length argument lists.
Example 4:
This example shows how to call a method of an object in the context of another object with the apply method.
Example 5:
This example shows how to call a method of an object in the context of another object with the call method.
Example 6:
In some cases, a code snippet needs to be evaluated at runtime.
For example, when the response of an AJAX request contains some source code that needs to be executed by the JavaScript interpreter.
In that case, we can use both the eval method and the Function object to solve the problem.
The prefered solution is to use the Function object, because it is much more faster than the eval method (see eval for details).
This example uses the eval method:
Example 7:
This example uses the Function object to implement the same functionality as the previous example:
|
External links:
User Contributed Comments