home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Function Object

Name

Function Object---a JavaScript function

Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

Synopsis

function functionname(argname1 [, ... argname_n])
{
     body
}

Constructor

new Function([argname1 [, ..., argname_n]], body) Navigator 3.0 only

Arguments

argname1, ..., argname_n

Any number of string arguments, each one of which names one argument of the Function object being created.

body

A string that specifies the body of the function. It may contain any number of JavaScript statements, separated with semicolons.

Returns

The newly created Function object.

Properties

arguments[]

An array of arguments that were passed to the function.

arguments.length

The number of elements in the arguments[] array.

caller

A reference to the Function object that invoked this this one, or null if the function is invoked at the top level.

prototype

An object which, for constructor functions, defines properties and methods that will be shared by all objects created with that constructor function.

Methods

toString()

Return a string representation of the function.

Description

A function is a fundamental data type in JavaScript. Chapter 6, Functions, explains how to define and use functions. The Function object is an object wrapper around the basic function data type; this object type exists so that functions can have properties and methods associated with them. When a function value is used in an "object context"--i.e., when you attempt to invoke a method or read a property of a function, JavaScript internally converts the function value into a temporary Function object, so that the method can be invoked or the property value read.

In Navigator 3.0, you can also use the Function() constructor method shown above to create your own Function objects. Functions defined in this way are sometimes called "anonymous" functions, because they are not given a name when they are created. Chapter 6, Functions discusses the definition and use of anonymous functions. Just as JavaScript converts from a function value to a Function object whenever necessary, so it will convert from a Function object (created with the Function() constructor) to a function value whenever you use the object in a function value context--i.e., whenever you invoke it with the () operator. This conversion from Function object to function value is done by the valueOf() method.

The arguments[] and caller properties of the Function object are unusual because they may only be accessed from within the body of a function. That is, they are only defined for a Function object while that function is being executed. There is no special keyword in JavaScript that refers to the currently executing function, so you must refer to Function objects by name. For example:

function myfunc() 
{ 
    if (myfunc.arguments.length == 0) return; 
        ...
}

Because JavaScript is loosely typed language, it does not care what number or type of arguments you pass to a function, even if you provided argument names when you defined the function. The arguments[] property is an array containing whatever arguments were passed to the function. You can use this array to implement functions that can handle any number of arguments correctly.

The caller property refers to the function that called this one. You can print out the caller for debugging purposes, and you can even use this property to invoke the calling function--creating a strange sort of recursion.

Note that the arguments property is actually just a reference to the Function object itself. Therefore, instead of typing function.arguments[i] and function.arguments.length you can just use function[i] and function.length. Although using the arguments property is unnecessary, it often makes for clearer code, and is the officially supported way to access function arguments.

Finally, the prototype property of the Function object is a very powerful one. It refers to a "prototype" object from which other objects "inherit" properties and methods. When a function is used as a constructor function, any objects created with that constructor will inherit the properties and methods defined in the prototype object of the constructor. Prototype objects and the prototype property are discussed in Chapter 7, Objects.


Previous Home Next
frames[] Book Index Function.arguments[]

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell