Swadon Swadon - 1 year ago 99
Javascript Question

Get parent class name from child with ES6?

I would like to get the parent class name (

), but I'm only able to retrieve the child class name with this code (

'use strict';

class Parent {


class Child extends Parent {


var instance = new Child();

Is it possible ?

Thanks !

Answer Source

ES6 classes inherit from each other. So when instance.constructor refers to the Child, then you can use Object.getPrototypeOf(instance.constructor) to get the Parent, and then access .name:

Object.getPrototypeOf(instance.constructor).name == "Parent";

Of course, full ES6 compliance and non-minified code are necessary for this to work. You should never rely on function names in code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download