The with() Statement
       by Kevin Kelly aka bear  |  3 March 2006

Often times, you will find yourself assigning many properties to one particular movie clip. Let's say you have a movie clip instance called theContent_txt. Adding properties to it could look like this:

theContent_txt.text = "the business is mine";
theContent_txt._x = 456;
theContent_txt._y = 345;

Notice how you are repeating the word theContent. A better, cleaner method for referencing one particular object would be to use the with() statement.

The with() statement calls an object once and allows you to access all of the object's properties and methods with only one reference. For example, using the with() statement, the above lines of code can be written as the following:

text = "the business is mine!";
_x =456;
_y =345;

I refer to theContent_txt only once! The with() statement may not be versatile enough to carry event handlers, but it does the work of referencing almost any class or object for you.

The following is an example of it working with the top level class Mouse:


In the above code snippet, I used a top level class and called its hide method.

You can also call nested objects inside the original object. Here is a diagram to better help you visualize what I am referring to:

The code for calling the secondObject object using with() would be:

secondObject._x = 340;

Here, I used a direct path to gain access to the properties of the nested secondObject object.

So, there you have it. This is my quick, short take on using the with() statement. Beyond simply providing aesthetic value to your code, it helps you to easily group together and recognize object assignments.

Thank you for your time. If you have any questions, please post them on the forum.

Kevin Kelly


SUPPORTERS:'s fast and reliable hosting provided by Media Temple.