As it happens, like manually created instances, display objects created programmatically can also be assigned an instance name via the name variable. For example, the following code creates a TextField instance, gives it the instance name “price,” adds it to a container, and then retrieves a reference to it by name.
var t:TextField = new TextField( );
t.text = "$99.99";
t.name = "price"
var detailsPage:Sprite = new Sprite( );
detailsPage.addChild(t);
trace(detailsPage.getChildByName("price"));
// Displays: [object TextField]
The preceding code may appear convenient because it offers a way to access an object on the display list according to some programmer-determined label, rather than by object reference or depth position. However, using instance names in this way is prone to error because ActionScript does not require instance names to be unique and does not throw an exception for attempts to access nonexistent instance names. Hence, use of instance names with programmatically created display objects should be avoided. Instance names should typically be used only when referring to text field instances or instances of Library symbols created manually in the Flash authoring tool. Programmatically created display objects should always be accessed by reference. For example, the following code shows two versions of displayPrice( ), a hypothetical method that displays the price of a product. Both of the following versions of the method display the price in a TextField. In the first version (recommended), the TextField that will contain the price is passed as an object reference to the method. In the second version (discouraged), the DisplayObjectContainer containing the TextField is passed to the method, and the method retrieves a reference to the TextField by instance name.
// Recommended
public function displayPrice (priceField:TextField, price:Number):void
{
priceField.text = "$" + price;
}
// Discouraged
public function displayPrice (orderForm:Sprite, price:Number):void
{ TextField(orderForm.getChildByName("price")).text = "$" + price;
}
Wherever practical, display objects should be made available by reference to dependent parts of a program. In MXML, a display object’s instance name can be set via the id attribute, and named display objects can also be accessed by getChildByName( ). However, as with pure ActionScript applications, references are preferred over instance names.
source: Essential ActionScript 3.0 by Colin Moock
Example by Adobe:
while i was navigating the as3 componenet reference, I found a prety good example:
import flash.display.Sprite;
import flash.events.MouseEvent;
var container:Sprite = new Sprite();
addChild(container);
var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFF0000);
circle1.graphics.drawCircle(40, 40, 40);
circle1.addEventListener(MouseEvent.CLICK, clicked);</code>
var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x00FF00);
circle2.graphics.drawCircle(100, 40, 40);
circle2.addEventListener(MouseEvent.CLICK, clicked);
var circle3:Sprite = new Sprite();
circle3.graphics.beginFill(0x0000FF);
circle3.graphics.drawCircle(70, 80, 40);
circle3.addEventListener(MouseEvent.CLICK, clicked);
container.addChild(circle1);
container.addChild(circle2);
container.addChild(circle3);
addChild(container);
function clicked(event:MouseEvent):void {
var circle:Sprite = Sprite(event.target);
var topPosition:uint = container.numChildren - 1;
container.setChildIndex(circle, topPosition);
}
look how we passed the object reference cleanly!
var circle:Sprite = Sprite(event.target);