O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Expanding the Scope Chain via the with Statement

The with statement provides a shorthand way to refer to the variables and methods of an object without having to specify the object's name repeatedly. A with statement takes the general form:

with (object) {
  substatements
}

When an identifier is referenced within a with statement block, object is checked for the specified name—before the remainder of the scope chain is consulted. In other words, with temporarily adds object to the end of ActionScript's internal list of objects in the scope chain.

For example, to refer to the built-in Math class's PI variable, we normally use the following code:

Math.PI;

But using the with statement, we can to refer to the built-in Math class's PI variable without the preceding reference to the Math class:

with (Math) {   // Execute statements in the context of Math
  trace(PI);    // Displays: 3.1459... (because PI is defined on Math)
}

Some developers find the with statement convenient when writing code that makes repeated references to a particular object's variables and methods.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required