Search code examples
classdojoradix

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 Stage));
        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


Solution

  • 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