Question

I need to determine if one class descends (directly or indirectly) from another.

I can do

var testInstance : Object = new ClassA();
if (testInstance is ClassB)
    ...

but I hate creating an instance just to test ancestry. I hoped that if (ClassA is ClassB) would work, but it does not seem to.

AS help states

isPrototypeOf(theClass:Object):Boolean
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the
parameter.

I don't actually understand ActionScript prototypes (I think it might drive me insane), but I hope that Class objects have some way of tapping into their inheritance information.

Thanks

Was it helpful?

Solution

Prototypes are instances, either of the class (for class inheritance) or the parent class (for prototype inheritance). Try:

Object.prototype.extends=function (theClass:Object):boolean {
    return this.prototype instanceof theClass;
}

ClassA.extends(ClassB);

ActionScript 3 shoehorns prototype inheritance into class based inheritance. You don't use prototypes much anymore in Actionscript, but it's not a complex concept and has its uses, so you'd do well to study it. Read "History of ActionScript OOP support" and "The prototype object" from "Advanced topics"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top