to super or not to super

Sometimes thereĀ are reasons for not calling the base class constructor. A little trick in AS3.0 to not super() your base class is the following line of code:

if (0) super();

Comments

3 Responses to “to super or not to super”

  1. AJ on March 31st, 2008 15:28

    I actually do not understand why we should do this.
    Simply not having super(); should also work isn’t it?

  2. Sander Wichers on March 31st, 2008 15:33

    super(); will always get called! The only flexibility you have is the position where you want to execute it within your constructor!

    The if (0) trick makes Flash believe it will actually execute a super() call so it will not add super() dynamically to your script (it does this in preparsing state).

  3. Justin Ohms on August 1st, 2008 22:48

    Thanks YOU!
    This was exactly what I was looking for. I tried something like this “if(false){super;}” but I guess the pre-compiler was able to see through it. As far as one use-case when this is code is needed… overriding singleton classes such as the Cairngorm servicelocator.

Leave a Reply