JavaScript Function Class



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site








Quick Reference for the Function Class



The Function Class Reference Table
Native class
Inherits From Object
Constants Properties Methods Constructors
Syntax Arguments Returns
Throws
 
None Instance Properties
arguments[] deprecated
caller deprecated
length
apply()
call()
toString()
new Function(parameters..., body) parameters
body
Returns:
A new Function Object
Throws:
SyntaxError




Function Class Description

A word of warning. Creating function via the Function class is not the preferred method to define your function. In most cases, a better way to create your function is via the function declaration statement or the function literal statement. The problem with the Function class approach of creating your function is that of memory inefficiency. Each object instantiated from your Function class type will require it's own copy in memory. The function declaration statement or the function literal are only parsed once when the page is loaded and only one copy of the function exists in memory.

With the warning having been issued, JavaScript supports functions through the Function class. Function objects are instantiated from the Function native class via the Function constructor. The constructor for the Function class will take two arguments. The first argument describes the function's parameters. The second argument describes the body.


Function Class Constants

There are no explicit constants for the class.





Function Class Instance Properties





Function Class Inherited Properties

The Function class does inherit the following properties from the base Object class.





Function Class Instance Methods

The Function class does inherit some methods from the base Object class.

The explicit method toString of the Function class will override the corresponding method of the base Object class.

The call( ) method and the apply( ) method are nearly identical. The primary difference is how one specifies the arguments to the methods. The apply( ) method expects an array for the second argument. Whereas, the call( ) method expects the argument values to be specified individually as an argument list.


The Methods for the Function Class
Method Syntax / Arguments Returns Description / Example
 
apply( ) Syntax:
function.apply(thisobject, args)

Arguments:
thisobject: the object to which the function is to be applied
args: an array to be passed to function
Returns:
whatever function returns
functionName.apply(thisobject, args) The above statement behaves as follows:
  • functionName: the name of a function
    • apply() invokes this function
    • this function can be an instantiated object of the Function class type
    • or this function can be a named function created by a function declaration statement
    • the this keyword within the body of this function will refer to the thisobject in the arguments of the apply() method
  • apply( ): our method:
    • apply( ) invokes the functionName
    • apply( ) causes thisobject to use functionName as it own method
    • apply( ) returns the return value from functionName
    • apply( ) accepts two arguments
  • thisobject: the object to which functionName is to be applied:
    • thisobject becomes the value of the this keyword as used in the function body of functionName
    • thisobject behaves as though functionName were it's own method
    • thisobject is the first of two argument of the apply( ) method
  • args: an array of values to be passed to functionName:
    • args values must match the formal parameters expected in functionName
    • args may also point to an Arguments object; you will need to create this object with the appropriate argument values suited for functionName
    • args is the second of two argument of the apply( ) method
  • the method will throw a TypeError exception when the method is invoked on an object that is not a Function or the args argument is not an array

Example:
See an illustration at, "Using The apply( ) Method".
call( ) Syntax:
function.call(thisobject, args....)

Arguments:
thisobject: the object to which the function is to be invoked
args: an argument list
Returns:
whatever function returns
functionName.call(thisobject, args....) The above statement behaves as follows:
  • functionName: the name of a function
    • call() invokes this function
    • this function can be an instantiated object of the Function class type
    • or this function can be a named function created by a function declaration statement
    • the this keyword within the body of this function will refer to the thisobject in the arguments of the call() method
  • call( ): our invoking method:
    • call( ) invokes the functionName
    • call( ) causes thisobject to use functionName as it own method
    • call( ) returns the return value from functionName
    • call( ) accepts a variable number of arguments
  • thisobject: the object to which functionName is to be invoked:
    • thisobject becomes the value of the this keyword as used in the function body of functionName
    • thisobject behaves as though functionName were it's own method
    • thisobject is the first argument of the call( ) method
  • args: a list of values to be passed to functionName:
    • args values must match the formal parameters expected in functionName
    • args values must be separated by commas
  • the method will throw a TypeError exception when the method is invoked on an object that is not a Function.

Example:
See an illustration at, "Using The call( ) Method".
toString( ) Syntax:
function.toString( )

Arguments: None
Returns a string of a Function object. The toString( ) method behaves as follows:
  • returns a string version of the Function object
  • the returned value will be a string representation in the form of a function object
  • the returned value may be implementation specific

Example:
//declare a function function showcar(type, year) { document.write("Your car is a " + year + " " + type); } // execute the toString ( ) method document.write(showcar.toString()); //returns string: function showcar(type, year) { document.write("Your car is a " + year + " " + type + " "); }




Function Class Constructor





Function Class Constructor Example

var fArea = new Function("width", "height", "return width*height;");

Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson