Function
object constructorS2C Home « Globals « Function
Functions are created using the global object constructor aFunction = new Function()
as below, via declaration using the function
statement, or by
function expression using the function
operator.
All functions are Function
objects underneath the covers, but as mentioned above there are different ways to set up the function creation.
Description
Declares a Function
object using the constructor property.
- Function objects created this way are less efficient as they are parsed on function creation as opposed to being parsed with the rest of the JavaScript code.
Syntax
Signature | Description |
---|---|
aFunction = new Function ([arg1 [, arg2 | Declares a Function object using the constructor property.
|
Parameters
Parameter | Description |
---|---|
arg1,arg2,...,argN | Formal argument names to be used by the function. Arguments passed are treated as the names of the parameter identifiers of the function. The function is created with the arguments in the order in which they are passed and can have the following formats:
|
functionBody | A string holding the JavaScript statements that comprise the function definition. |
Class Properties
Class Property | Description |
---|---|
prototype | Enables property assignment to objects of type Function . |
Instance Properties
Instance Property | Description |
---|---|
constructor | Returns a reference to the Function function that created the prototype. |
length | A positive integer holding the number of arguments expected by the function. |
Class Methods
None.
Instance Methods
By using methods of the Function
object we can also access elements of functions, manipulate functions after creation and even create new functions from existing functions.
Instance Method | Description |
---|---|
Getter (Accessor) MethodsThese methods DO NOT modify the Function object |
|
apply() | Calls a function with a given this value and arguments provided as an array. |
call() | Calls a function with a given this value and arguments provided individually. |
toString() | Returns a string representation of the function. |
Setter (Mutator) MethodsThese methods DO modify the Function object |
|
None. |
Using The Function
Constructor
The Function
constructor allows us to create a Function
object.
Below is an example of using the Function
constructor.
*/
/ Create a Function that takes an argument and
/ returns the square of the argument.
/*
var newFunction = new Function('a', 'return a * a');
alert(newFunction(6));
alert(newFunction(12));
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 8 - Functions