
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Function Literals
|
191
In this example, the inner expression combine(5,6), which evaluates to 11, becomes
an argument to the outer combine() function call, where it is concatenated with the
string “ people were at the party”.
If a return statement doesn’t include an expression to be returned, or if
the return statement is omitted entirely, a function will return the
value
undefined. In fact, this is a common source of error.
For example, the following won’t do anything meaningful, because the return state-
ment is missing:
function combine (a, b) {
var result = a + b; // The result is calculated, but not returned
}
Likewise, this too is incorrect:
function combine (a, b) {
var result = a + b;
return; // You've forgotten to specify the return value
}
The correct function should read:
function combine (a, b) {
var result = a + b;
return result;
}
When creating a function that is supposed to return the result of a calculation, don’t
forget to include a return statement that actually returns the desired value. Other-
wise, the return value will be
undefined and any subsequent calculations based on
that result will almost certainly be incorrect.
Function Literals
ActionScript allows us to create function literals, which are convenient when we need
a function temporarily or when we want to use a function ...