In AS3, you can initialize a member variable (or constant) by calling a member function. This happens before the constructor is called. In the meantime, the ‘this’ keyword is perfectly accessible in the initializing member function even though the constructor function hasn’t been issued yet.
This sounds like a time bomb. Can anyone comment on the above practice?
Edit :
...
private var member:Sprite = getSprite(); // called before constructor
...
private function getSprite():Sprite {
var spr:Sprite = new Sprite();
this.addChild(spr); // 'this' used before constructor
return spr;
}
As I understand it, that’s fine (if not really nice and readable). What happens when new is called is:
thisbecomes available)newreturnsthisThe danger lies in that you have to make sure that nothing in
getSprite()requires something that is initialized in the constructor (including the parent constructor, if it’s called). I would avoid it and just initialize everything in the constructor instead.