tags:

views:

47

answers:

2

Hello,

I am using this to create a few classes and to set up some inheritance structure.

var ControlType = Class.extend({
    init: function(){ },

    html: function(type){
        //Get template based on name of subclass
    }
});

var YesNo = ControlType.extend({
    init: function(){ },
    html: function() {
        this._super('YesNo')
    }
});

Now I was wonder if it's possible to get the type of the subclass in ControlType.html without passing it explicitly?

Update:

I tried this.constructor without the extend functionality as someone suggested but this returns the type of the parent class. So in the following example the console logs Person() but I need Jimi().

function Person(){
    this.say = function(){
        console.log(this.constructor);
    }
}

function Jimi(){
    this.name = 'Jimi';
}
Jimi.prototype = new Person();

var j = new Jimi();
j.say();
A: 

It looks like you are using some kind of inheritance library. I'm not sure what extend does, but in plain JavaScript, you can use this.constructor to refer to the constructor function of an object.

casablanca
+1  A: 

Hard to pick since there are actually a few different ways you could do this. I guess maybe the easiest would be something like this:

function Person(){
    this.name = 'Person'; //<<< ADDED THIS
    this.say = function(){
        console.log(this.name);  //<<< ADDED THIS
    }
}

function Jimi(){
    this.name = 'Jimi';
}
Jimi.prototype = new Person();

var j = new Jimi();
j.say();

This will then output

Jimi

Hopefully it helps!

Ryan Alberts
Thanks for the answer. Seems I was heading to the same solution in my own code.
Pickels