Version 2, last updated by Kirill Mourzenko at September 05, 2008 15:22 UTC

AS2 is advertised as object oriented but in reality the object orientedness is a giant hack built on top of AS1 which is not OO. There exists a major a bug in the super operator which appears when you use apply or call methods of a function to execute it. Take this scenario:

class Top
{
      function f()
      {
           trace("TOP")
      }
}

 class Middle
 {
      function f()
      {
           super.f();
           trace("MIDDLE")
      }
 }

 class Bottom
 {
 }

In normal OO world doing this:

 var o : Object = new Bottom();
 o.f();

Would produce this result:

 TOP
 MIDDLE

And that's what you get when you call the function directly on the object. But doing this, which Flash boasts as being the same as calling the function directly:

 o.f.call( o );

Produces this result:

 TOP
 MIDDLE
 MIDDLE

So what's going on? I don't know much about the internals of flash but I can guess. call and apply are executed on the object you pass in as the target, which basically means you can execute any function as a method of an object even if it's not a method of the object. In the case above f is not exactly a method of the Bottom class, it doesn't have it in it's prototype, but in its parent's prototype. So in executing f it doesn't dereference it as a method of its superclass, it executes it as a method of Bottom. So when the f method uses the super operator it comes back with Bottom's parent which is Middle instead of Top and calls its f method which is exactly the same as the one in which super got used, thus causing two trace of MIDDLE.

This is a problem that stems from call and apply but the cause is inside the super operator. I fixed this by providing my own implementation. All HATObjects have access to it by saying Super.method() or Super.prop = val or var o = Super.prop. The only place where you should continue to use the built in super operator is the constructor.