Section 3.4. Functions


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 datatypes. 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. Defining and invoking functions, as well as how to use them as data values, are discussed in Chapter 8.

Since functions are values just like numbers and strings, they can be assigned to object properties just as other values can. When a function is assigned to a property of an object (the object datatype 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. You'll see more about them in Chapter 7.

3.4.1. Function Literals

The preceding section defined a function called square( ). The syntax shown there is used to define most functions in most JavaScript programs. However, JavaScript also provides a syntax 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 then 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; } 

it can be defined 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 you might choose to use function literals in a program, I'll show you later how 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 are less efficient than functions defined in either of the other two ways.




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net