home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam

## 15.3. Advanced Function Scope Issues

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:

```var rotateAmount = 45;
var widthAmount = 50;

function transformClip ( ) {
_rotation = rotateAmount;
_xscale = widthAmount;
}

// Invoke the function
transformClip( );```

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```

#### Example 15-4. The Fixed Scope of Object Methods

```// Set our variables
var rotateAmount = 45;
var widthAmount = 50;

// Create a transformClip() function
// that prints the value of rotateAmount and widthAmount
function transformClip ( ) {
trace(rotateAmount);
trace(widthAmount);
}

// Create an object that corresponds with
// the rect clip in the previous example
var rectObj = new Object( );

// Copy transformClip to a property of rectObj
rectObj.tc = transformClip;

// Set rotateAmount and widthAmount properties on rectObj
rectObj.rotateAmount = 15;
rectObj.widthAmount = 10;

// Now invoke rectObj.tc
rectObj.tc( );  // Displays 45 and 50, not 15 and 10. The scope of
// rectObj.tc is the same as transformClip().```

#### Example 15-5. JavaScript's Static Function Scope

```// CODE ON FRAME 0 OF A FRAMESET
// Assign a variable in frame 0
var myVar = "frame 0";

// Set a function in frame 1 of the frameset, and copy that
// function back to frame 0
parent.frames[1].myMeth = function ( ) { alert(myVar); };
myMeth = parent.frames[1].myMeth;

// Invoke myMeth() from frame 0
myMeth( ); // displays "frame 0"

// CODE ON A BUTTON IN FRAME 1 OF THE SAME FRAMESET
<FORM>
<INPUT type="button" value="click me" onClick="myMeth( );">
</FORM>

// Now click the button to invoke myMeth( ) from frame 1.
// "frame 0" is displayed.
// myMeth( ) is not scoped to frame 1, but to frame 0, where
// the declaration statement occurred.```

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