start page | rating of books | rating of authors | reviews | copyrights

JavaScript: The Definitive GuideJavaScript: The Definitive GuideSearch this book

3.4. Functions

A function is a piece of executable code that is defined by a JavaScript program or predefined by the JavaScript implementation. Although a function is defined only once, a JavaScript program can execute or invoke it any number of times. A function may be passed arguments, or parameters, specifying the value or values upon which it is to perform its computation, and it may also return a value that represents the results of that computation. JavaScript implementations provide many predefined functions, such as the Math.sin( ) function that computes the sine of an angle.

JavaScript programs may also define their own functions with code that looks like this:

function square(x)  // The function is named square. It expects one argument, x.
{                   // The body of the function begins here.
  return x*x;       // The function squares its argument and returns that value.
}                   // The function ends here. 

Once a function is defined, you can invoke it by following the function's name with an optional comma-separated list of arguments within parentheses. The following lines are function invocations:

y = Math.sin(x);
y = square(x);
d = compute_distance(x1, y1, z1, x2, y2, z2);
move( ); 

An important feature of JavaScript is that functions are values that can be manipulated by JavaScript code. In many languages, including Java, functions are only a syntactic feature of the language -- they can be defined and invoked, but they are not data types. The fact that functions are true values in JavaScript gives a lot of flexibility to the language. It means that functions can be stored in variables, arrays, and objects, and it means that functions can be passed as arguments to other functions. This can quite often be useful. We'll learn more about defining and invoking functions, and also about using them as data values, in Chapter 7.

Since functions are values just like numbers and strings, they can be assigned to object properties just like other values can. When a function is assigned to a property of an object (the object data type and object properties are described in Section 3.5), it is often referred to as a method of that object. Methods are an important part of object-oriented programming. We'll see more about them in Chapter 8.

3.4.1. Function Literals

In the preceding section, we saw the definition of a function square( ). The syntax shown there is used to define most functions in most JavaScript programs. However, ECMAScript v3 provides a syntax (implemented in JavaScript 1.2 and later) for defining function literals. A function literal is defined with the function keyword, followed by an optional function name, followed by a parenthesized list of function arguments and the body of the function within curly braces. In other words, a function literal looks just like a function definition, except that it does not have to have a name. The big difference is that function literals can appear within other JavaScript expressions. Thus, instead of defining the function square( ) with a function definition:

function square(x) { return x*x; } 

We can define it with a function literal:

var square = function(x) { return x*x; } 

Functions defined in this way are sometimes called lambda functions in homage to the LISP programming language, which was one of the first to allow unnamed functions to be embedded as literal data values within a program. Although it is not immediately obvious why one might choose to use function literals in a program, we'll see later that in advanced scripts they can be quite convenient and useful.

There is one other way to define a function: you can pass the argument list and the body of the function as strings to the Function( ) constructor. For example:

var square = new Function("x", "return x*x;"); 

Defining a function in this way is not often useful. It is usually awkward to express the function body as a string, and in many JavaScript implementations, functions defined in this way will be less efficient than functions defined in either of the other two ways.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.