views:

34

answers:

1

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

A: 

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"

outis
It looks like I would either have to modify the class (which I might not have access to) or create an instance of a class just to test its ancestry.Thanks for the code; I will play with it.
Richard Haven
If you don't compile in strict mode, or if the class is declared dynamic, you shouldn't need the source code to modify the class. Furthermore, you should be able to add methods to `Object` as shown, which all objects will inherit.
outis