Skip to content

Instantly share code, notes, and snippets.

@robertpenner
Created April 29, 2010 18:19
Show Gist options
  • Save robertpenner/383991 to your computer and use it in GitHub Desktop.
Save robertpenner/383991 to your computer and use it in GitHub Desktop.
Inheritance chain hacking in AS2
/**
* Makes an object inherit from a class.
* Modifies the inheritance chain and calls the class constructor.
* Commonly used to make a movie clip such as the root timeline
* inherit from a custom class, e.g.
* // on main timeline
* applyClassToInstance(MyDocumentClass, this);
* Works for AVM1 only, i.e. AS1 and AS2.
* @param theClass
* @param theInstance
* @param constructorArgs An optional Array of parameters for the class constructor.
*/
function applyClassToInstance(theClass:Function, theInstance:Object, constructorArgs:Array):Void
{
theInstance.__proto__ = theClass.prototype;
theInstance.__constructor__ = theClass;
// run the constructor
theClass.apply(theInstance, constructorArgs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment