I have two type definitions, how can I determine if one is a base type of the other?
I have two type definitions (references of type Class) in ActionScript 3 and I need to figure out if one is a base type (class or interface) of the other.
I had hoped something like the following would work, but alas it did not:
var isBaseClass:Boolean = MouseEvent is Event;
It is 开发者_JAVA技巧understandable why it doesn't, but it would still be nice. I can't use describeType either, since for objects of type Class it doesn't actually return the correct inheritance chain but rather just return the types Class and Object, which doesn't help at all. I can use getQualifiedSuperClassName in a loop until either there are no more super classes or there is a match, but it's less than ideal and doesn't work for type checking against interfaces.
Anyone have a better idea?
Here a live example using describeType with no problem : http://wonderfl.net/c/h90R
trace(describeType(MouseEvent)..extendsClass)
output :
<extendsClass type="Class"/>
<extendsClass type="Object"/>
<extendsClass type="flash.events::Event"/>
<extendsClass type="Object"/>
I saw this suggested somewhere but haven't really tried it myself in practice:
var classA:Class = Event;
var classB:Class = MouseEvent;
trace(classA.prototype.isPrototypeOf(classB.prototype));
var classA:Class = Event;
var classB:Class = MouseEvent;
trace(classA.prototype.isPrototypeOf(classB.prototype));
As answered by Lars, this works for me. Make sure the base class is first (classA
). isPrototypeOf
is analogous to isSuperClassOf
.
精彩评论