Book HomeActionScript: The Definitive GuideSearch this book

9.11. Functions as Objects

In ActionScript, functions are technically a special type of built-in object. Let's see what that means and how it affects what you can do with functions.

9.11.1. Passing Functions to Functions

Perhaps surprisingly, we can use any function as an argument to another function like this:

function1(function2);

Note that if there are no parentheses following function2, the interpreter doesn't execute function2( ) but instead just passes its "object reference" as an argument to function1( ). That is, function1( ) receives function2 itself, not the return 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 internal stop( ) function (stops the current movie)
doCommand(play);  // Pass the internal play( ) function (plays the current movie)

Because functions are a type of object, we may treat them like any other data. In the following example, we assign the internal gotoAndPlay function to the variable gp, which gives us a shorter way to refer to the function:

gp = gotoAndPlay;  // Create a shortcut reference to gotoAndPlay( )
gp(25);            // Invoke gotoAndPlay( ) using our reference

In addition to passing and storing functions as objects, we can exploit the "objectness" of functions by attaching properties to them, like this:

// Create a function
function myFunction ( ) {
  trace(myFunction.x);
}

// Attach a property to it
myFunction.x = 15;

// Check the property value by invoking the function
myFunction( );  // Displays: 15

TIP

By attaching properties to functions, we can maintain the state of a piece of information between function executions without cluttering up a timeline with variables.

Function properties offer the benefits of local variables without expiring between function invocations. This is useful when a function needs to be invoked with a unique identifier. Here, for example, is a generic function that duplicates a movie clip and gives the duplicated clip a unique name and level:

makeClip.count = 0;    // Define a property of makeClip( ) (remember that 
                       // makeClip( ) already exists because functions are
                       // defined before code runs)

// Duplicate a passed clip and assign the new clip an automatic name
function makeClip (theClip) {
  // Add one to our clip counter
  makeClip.count++

  // Now duplicate the clip, assigning it a unique name and depth
  theClip.duplicateMovieClip(theClip._name + makeClip.count, makeClip.count);
}

makeClip(square);   // Make a duplicate of square using makeClip( )

square1._x += 100;  // Now move the duplicated square to the right


Library Navigation Links

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