T-W > with
withSyntax
with (object
) {statement(s)
;
}
Arguments
object
An instance of an ActionScript object or movie clip.
statement(s)
An action or group of actions enclosed in curly braces.
Description
Action; temporarily changes the scope (or target path) used for evaluating expressions and actions in the statement(s)
. After the with
action executes, the scope chain is restored to its original state.
The object
becomes the context in which the properties, variables, and functions are read. For example, if object
is myArray
, and two of the properties specified are length
and concat
, those properties are automatically read as myArray
.length
and myArray.concat
. In another example, if object
is state.california
, it is as if any actions or statements inside the with
action were called from inside the california
instance.
To find the value of an identifier in the statement(s)
, 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
action to resolve identifiers starts with the first item in the following list and continues to the last, as follows:
![]() |
object referenced by innermost with action |
![]() |
object referenced by outermost with action |
![]() |
Activation object (A temporary object that is automatically created when a function is called that holds the local variables called in the function.) |
![]() |
Movie clip containing currently executing script |
![]() |
Global object (predefined objects such as Math, String) |
In Flash 5 the with
action replaces the deprecated tellTarget
action. You are encouraged to use with
instead of tellTarget
because it is a standard ActionScript extension to the ECMA-262 standard. The principal difference between the with
and tellTarget
actions is that with
takes a reference to a movie clip or other object as its argument, while tellTarget
takes a target path string identifying a movie clip, and cannot be used to target objects.
To set a variable inside a with
action, the variable must have been delared outside the with
action 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
action without having declared it, the with
action 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
action was called.
Example
The following example sets the x and y properties of the someOtherMovieClip
instance, and then instructs someOtherMovieClip
to go to frame 3 and stop:
with (someOtherMovieClip) { _x = 50; _y = 100; gotoAndStop(3); }
The following code snippet is how you would write the preceding code without using a with
action:
someOtherMovieClip._x = 50; someOtherMovieClip._y = 100; someOtherMovieClip.gotoAndStop(3);
This code could also be written using the tellTarget
action:
tellTarget ("someOtherMovieClip") { _x = 50; _y = 100; gotoAndStop(3); }
The with
action 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 since they exist in the object activation scope of the function polar
, they resolve to the corresponding local variables.
function polar(r){ var a, x, y with (Math) { a = PI * r * r x = r * cos(PI) y = r * sin(PI/2) } trace("area = " +a) trace("x = " + x) trace("y = " + y) }
You can use nested with
actions to access information in multiple scopes. In the following example, the instance fresno
and the instance salinas
are children of the instance california
. The statement sets the _alpha
values of fresno
and salinas
without changing the _alpha
value of california
.
with (california){ with (fresno){ _alpha = 20; } with (salinas){ _alpha = 40; } }
See also
tellTarget