Actionscript WITH Statement

I recently discovered a nice little time saver in Actionscript, the WITH statement. Too often, do I find myself having to type the same object names over and over (many times I often use copy/paste) when assigning properties. The WITH statement though allows you shorten your syntax up.

Say you have an object, myObjectName, and you need to set the x, y, alpha, width, and height properties of this object. Traditionally I would write this as follows:

myObjectName.x = 0;
myObjectName.y = 0;
myObjectName.alpha = 1;
myObjectName.width = 100;
myObjectName.height = 100;

Using the WITH statement I can shorten my key strokes up by using the following syntax:

with(myObjectName)
{ x = 0;
y = 0;
alpha = 1;
width = 100;
height = 100;
}

I'm not sure how often I will personally use this statement (kind of makes your code look messy/ugly) but you never know when it may come in handy.

UPDATE: Actionscript 3.0 Reference - Statement, Keywords, & Directives

Vote on HN


comments powered by Disqus