开发者

Dojo instanceof returning true for parent class

I have three classes Container, Stage and View. Stage inherits from Container, View also inherits from Container. When I call instanceof on an instance of the View class (View object), I get the following results

dojo.declare("View", [Container] , {
    constructor: function(){
        console.log(this.name + ' is a container-> ' + (this instanceof Container));
        console.log(this.name + ' is a View-> ' + (this instanceof View));
        console.log(this.name + ' is a Stage-> ' + (this instanceof 开发者_StackOverflowStage));
        this.preLoad();
    },
});

The output is

XYZ is a container -> true
XYZ is a View -> true
XYZ is a Stage -> false

How do I find the child class name/type?

this provides a work around for multiple inheritance, but it's not what i am looking for


You can find a possible solution a bit further in the link you provided: http://dojotoolkit.org/reference-guide/dojo/declare.html#meta-information

Basically, any object created from a declare'd class has a declaredClass attribute, if you named the class. So you can do:

dojo.declare('ns.Foo', [], {});
dojo.declare('ns.Bar', [ns.Foo], {});

var x = new ns.Bar();
console.log(x.declaredClass == 'ns.Bar');  // true
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜