The ‘with’ Operator

I have yet to personally see this implemented in code, so in hopes of remembering it or somehow promoting it’s popularity… I present ActionScript’s ‘with’ operator.

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

Establishes a default object to be used for the execution of a statement or statements, potentially reducing the amount of code that needs to be written.

The object parameter becomes the context in which the properties, variables, and functions in the statement(s) parameter are read. For example, if objectis 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 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:

  • The object specified in the object parameter in the innermost with statement
  • The object specified in the object parameter in the outermost with statement
  • The Activation object (a temporary object that is automatically created when the script calls a function that holds the local variables called in the function)
  • The object that contains the currently executing script
  • The Global object (built-in objects such as Math and String)

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.

Parameters

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

Example
How to use this 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);
/* area = 28.2743338823081 x = -3 y = 3 */

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s