View comments | RSS feed

with statement

with (object:Object) { 
statement(s); 
}

Lets you specify an object (such as a movie clip) with the object parameter and evaluate expressions and actions inside that object with the statement(s) parameter. This prevents you from having to repeatedly write the object's name or the path to the object.

The object parameter becomes the context in which the properties, variables, and functions in the statement(s) parameter are read. For example, if object is my_array, and two of the properties specified are length and concat, those properties are automatically read as my_array.length and my_array.concat. In another example, if object is state.california, any actions or statements inside the with statement are called from inside the california instance.

To find the value of an identifier in the statement(s) parameter, ActionScript starts at the beginning of the scope chain specified by the object and searches for the identifier at each level of the scope chain, in a specific order.

The scope chain used by the with statement to resolve identifiers starts with the first item in the following list and continues to the last item:

To set a variable inside a with statement, you must have declared the variable outside the with statement, or you must enter the full path to the Timeline on which you want the variable to live. If you set a variable in a with statement without declaring it, the with statement will look for the value according to the scope chain. If the variable doesn't already exist, the new value will be set on the Timeline from which the with statement was called.

Instead of using with(), you can use direct paths. If you find that paths are long and cumbersome to type, you can create a local variable and store the path in the variable, which you can then reuse in your code, as shown in the following ActionScript:

var shortcut = this._parent._parent.name_txt; shortcut.text = "Hank"; shortcut.autoSize = true;

Availability: ActionScript 1.0; Flash Player 5

Parameters

object:Object - An instance of an ActionScript object or movie clip.

Example

The following example sets the _x and _y properties of the someOther_mc instance, and then instructs someOther_mc to go to Frame 3 and stop.

with (someOther_mc) { 
 _x = 50; 
 _y = 100; 
 gotoAndStop(3); 
} 

The following code snippet shows how to write the preceding code without using a with statement.

someOther_mc._x = 50; 
someOther_mc._y = 100; 
someOther_mc.gotoAndStop(3);

The with statement is useful for accessing multiple items in a scope chain list simultaneously. In the following example, the built-in Math object is placed at the front of the scope chain. Setting Math as a default object resolves the identifiers cos, sin, and PI to Math.cos, Math.sin, and Math.PI, respectively. The identifiers a, x, y, and r are not methods or properties of the Math object, but because they exist in the object activation scope of the function polar(), they resolve to the corresponding local variables.

function polar(r:Number):Void { 
 var a:Number, x:Number, y:Number; 
 with (Math) { 
 a = PI * pow(r, 2); 
 x = r * cos(PI); 
 y = r * sin(PI / 2); 
 } 
 trace("area = " + a); 
 trace("x = " + x); 
 trace("y = " + y); 
} polar(3);

The following result is displayed in the Output panel.

area = 28.2743338823081 
x = -3 
y = 3 

Flash CS3


Comments


automataStef said on Feb 2, 2008 at 6:33 AM :
According to the docs, the scope chain used by the 'with'-statement to resolve identifiers is: innermost with statement->outermost with statement->activation object->movieclip->global object. If that would have been true, the following code should output "propOfMyObject"

var foo:String = "propOfActivationObject";
var myObject:Object = {foo:"propOfMyObject"};
with (myObject) {
trace(foo); //outputs "propOfActivationObject"!
}

But it doesn't. Instead it outputs "propOfActivationObject". It seems like local variables are shadowing the myObject in the 'with'-statement. So it seems like the scope chain rather is: activation object -> innermost with statement->outermost with statement->movieclip->global object. Am I right?
Francis Cheng said on Feb 8, 2008 at 1:22 PM :
That's interesting behavior that you found there, but the docs are correct. The purpose of the with statement is to push all of the properties of the target object to the front of the scope chain. What you are seeing seems to me to be a bug. I could reproduce this behavior only in FlexBuilder, though. The same code snippet produces propOfMyObject in both Flash 8 and Flash CS3. I've filed a bug about this in JIRA, we'll see how it goes:

http://bugs.adobe.com/jira/browse/ASC-3156

Thanks for pointing this out.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001343.html