15.3. Advanced Function Scope IssuesThis section describes another advanced topic that was too esoteric for our initial discussion about function scope. Let's revisit it here now that we've learned about movie clips, function scope, and objects. We learned in Chapter 9, "Functions" that a function's scope chain is normally determined relative to the function's declaration statement. There is, however, a subtle extension to this rule. When a function from one timeline is assigned to a variable in a different movie clip's timeline, that assignment also affects the function's scope chain. If the original function is invoked directly, its scope chain includes its original timeline, but if the function is invoked through the variable, its scope chain includes the variable's timeline. For example, suppose we create a function called transformClip( ) that rotates and scales the current clip. We set the amount to rotate and scale the clip in the variable's rotateAmount and widthAmount:
Next we assign transformClip to a variable, tc, in a clip called rect: rect.tc = transformClip; When we invoke transformClip through rect.tc, as follows, nothing happens: rect.tc( ); Why? The function stored in tc has a scope chain that includes rect, not our original function's timeline, so rotateAmount and widthAmount are not found. But when we add rotateAmount and widthAmount variables to rect, the function can find the variables, so it works: rect.widthAmount = 10; rect.rotateAmount = 15; rect.tc( ); // Sets rect to 10 percent width, and 15 degrees rotation In contrast, when regular data objects on the same timeline are involved in function assignment, the assigned function's scope chain is not altered; rather, the function's scope chain is permanently determined relative to the function declaration. Example 15-4 demonstrates. Example 15-4. The Fixed Scope of Object Methods
When assigned to an object property, a function is scoped to the timeline that bears the function declaration. However, when assigned to a remote movie clip, a function is scoped to the timeline of the remote clip. Note that this behavior is actually a departure from JavaScript, in which functions are permanently scoped to objects based on the function declaration. For example, if we assume the frames of an HTML frameset to be roughly analogous to the clips in a Flash movie, then we can see the discrepancy; in JavaScript, a function assigned to a remote frame is still scoped to the frame with the function declaration, not the remote frame, as shown in Example 15-5. Example 15-5. JavaScript's Static Function Scope
Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|