Javascript: get subclass type in superclass

I use this to create several classes and create 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 wondering if it is possible to get the subclass type in ControlType.html without passing it explicitly?

Update:

I tried this.constructor without an extension of functionality, as someone suggested, but this returns the type of the parent class. So, in the following example, the console registers 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(); 
+4
source share
2 answers

It’s hard to pick up, as there are actually several different ways to do this. I think maybe the simplest 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(); 

Then it will be displayed

 Jimi 

Hope this helps!

+3
source

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 reference the constructor function of an object.

0
source

Source: https://habr.com/ru/post/1316033/


All Articles