is it possible to obtain the instance name of a class from the class without having to manually pass the instance name as a string parameter to the class constructor?
//Create New SizeClass
var big:SizeClass = new SizeClass();
//-------------
package
{
public class SizeClass
{
public function SizeClass()
{
trace( //-- Instance Name "big" --// );
}
}
}
No, it is not possible to know anything about the containing code block during a constructor, save what you can learn from the stack trace (though that’s not available except in the debugger version of Flash). Even if you had a global access point for the containing class, it still would not allow for that access.
Think of a constructor like a method call. In a line of AS, it will be called before the assignment. Eg:
var a:Foo = new Foo()theFoois created (the constructor completes), and thenais populated with whatever just happened. After that pointawill remain agnostic of its context (because of encapsulation) unless it is told about it (this is even true on a DisplayObject — try this(var mc:MovieClip = new MovieClip(); trace( mc.root ) //this will be null).I’m keeping this because it is useful albeit not useful to your original answer.
You can always get the name of a class with
getQualifiedClassNamefrom theflash.utilspackage. You can’t get a DisplayObject’s until well after it has been constructed, but you can simulate this by (I believe) overridingfunction set name( value:String ):void. If that doesn’t work, then try finding it afterEvent.ADDEDand/orEvent.ADDED_TO_SAGE.