
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
210
|
Chapter 9: Functions
value of function2(). Because objects are passed by reference, we can pass a function
identifier to another function and it will arrive unscathed. The passed function can
be executed like this:
function doCommand (command) {
command( ); // Executes the passed function
}
// Some examples:
doCommand(stop); // Pass the built-in stop() function (stops current movie)
doCommand(play); // Pass the built-in play() function (plays current movie)
We can use this technique to assign a callback function to a series of text field
objects:
function initFormFields (fields, callbackFunc) {
// Set focus callback functions for text fields.
for (var i = 0; i < fields.length; i++) {
fields[i].onSetFocus = callbackFunc;
}
}
// Create a list of the text fields.
editFields = [firstNameField, lastNameField, emailField,
phoneField, companyField, commentsField];
// Function that colors a text field blue.
function handleFieldFocus ( ) {
this.background = true;
this.backgroundColor = 0x333399;
}
initFormFields(editFields, handleFieldFocus);
Storing Functions in Variables
Because functions are a type of object, we can treat them as we treat any other data.
In the following example, we assign the built-in gotoAndPlay function to the variable
gp, which gives us a shorter way to refer to the function: ...