Team LiB
Previous Section Next Section

Functions as Data

Because a function is considered an object by the JavaScript interpreter, it can be assigned as the value of a variable. You have seen examples of this before, when extending the functionality of built-in JavaScript objects such as Array and String objects. The following example will look familiar; it has been presented several times already:

function display()
{
  for( i = 0 ; i < this.length ; i++ )
  {
    document.write( this[i] + "<br>" );
  }
}
Array.prototype.display = display;

This function extends the functionality of Array objects declared anywhere on the window in which the function is defined. The new object's method can then be used just like any other built in-method for that object type.

var myArray = new Array( 12, 42, 1001, 10 );

myArray.display();

will produce the following output:

As you can see, the ability to use functions as data is a great advantage. Using functions will shorten the length of your programs and make them easier to follow.

Function-Specific Properties

Because functions are essentially objects, they have several built-in properties that are quite useful for retrieving information about a function.

arguments

The arguments property returns an array containing the values of each argument that was passed to the function, in the order in which they were passed.

Following is an example of using the arguments property:

function myFunction( param0, param1, param2 )
{
  for( i = 0 ; i < myFunction.arguments.length ; i++ )
  {
    document.write( myFunction.arguments[i] + "<br>" );
  }
}

The output of this code segment would be the values of each parameter that was passed to the myFunction function.

arity

The arity property returns an integer whose value is the number of parameters defined for the function.

Following is an example of using the arity property:

function myFunction( param0, param1, param2 )
{
  for( i = 0 ; i < myFunction.arity ; i++ )
  {
    document.write( myFunction.arguments[i] + "<br>" );
  }
}

The output of this code segment would be the values of each parameter that was passed to the myFunction function.

caller

The caller property returns a reference to the function that invoked the current function.

Following is an example of using the caller property:

function myFunction()
{
  if( myFunction.caller == someFunction )
  {
 <statements>
  }
}

length

The length property returns an integer whose value is the number of parameters defined for the function. (Netscape Navigator always returns a value of zero—refer to the arity property section presented previously.)

Following is an example of using the length property:

function myFunction( param0, param1, param2 )
{
  for( i = 0 ; i < myFunction.length ; i++ )
  {
    document.write( myFunction.arguments[i] + "<br>" );
  }
}

The output of this code segment would be the values of each parameter that was passed to the myFunction function.

prototype

The prototype property is a static property of the function object. Use the prototype property to assign new properties and methods to the function object for the current document.

To add an additional method to the function object, all you'd need to do is write the corresponding function:

function print()
{
  document.write( "Parameters: " + this.arity + " Caller: " +
                    this.caller + " String value: " + 

                    this.toString() + "<br>" );
}

Then simply add it to the prototype property of the static function object:

Function.prototype.print = print;

Then, whenever a new instance of the function object is created, a new method, print(), will be available to it:

function myFunction( param0, param1, param2 )
{
...
}

myFunction.print();

When the print() method is called, the information for the function will be output.

Function Methods

The methods presented in the following subsections are part of every object, including function objects.

tostring()

The tostring() method returns the function's value as a string object. This method is generally not useful because the JavaScript interpreter converts all objects to their string representation before outputting them.

Following is an example of using the toString() method:

function myFunction()
{
...
}
document.write( myFunction.toString() );

The last statement would be equivalent to

document.write( myFunction );

They both output the string representation of the function object.

valueOf()

The valueOf() function returns a reference to the function object.

Following is an example of using the valueOf() method:

var funcRef = someFunction.valueOf();

This will set the value of funcRef equal to a reference to the someFunction function.


Team LiB
Previous Section Next Section