JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow Function

Function constructor

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
                    [, ... argN]) functionBody;)
Declares a Function object using the constructor property.
  • Omitting the new special operator has the same effect as invoking Function as a constructor

Parameters


Parameter Description
arg1,arg2,...,argNFormal 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:
  • Comma(,) delimited list of strings that correspond to valid JavaScript identifiers ('a', 'b', 'c',...)
  • Comma(,) delimited string where each entry corresponds to a valid JavaScript identifier ('a, b, c...')
functionBodyA string holding The JavaScript statements that comprise the function definition.

Class Properties


Class Property Description
prototypeEnables property assignment to objects of type Function.

Instance Properties


Instance Property Description
constructorReturns a reference to the Function function that created the prototype.
lengthA positive integer holding the number of arguments expected by the function.

Class Methods

None.

Instance Methods


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));

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 8 - Functions



  

go to home page Homepage go to top of page Top