1

I have a parent class in JS that has some properties of which some are based on the class name of the child classes:

ParentClass.js

export default class ParentClass {

    constructor(className) {

        this.className = className;
        this.classNameLowerCase = className.toLowerCase();
    }
}

In a child class I extend the parent class and use super() for the constructor invocation where I will pass the class name of the child class.

ChildClass.js

import ParentClass from  "./ParentClass.js";


class ChildClass extends ParentClass {

    constructor() {
        super("ChildClass");
    }
}

As you can see the child class is called ChildClass and I also pass this name as a string in super(). I prefer to get the class name of the child class with a funtion (without havind to repeat myself). Without using super() I could use this.constructor.name to get the name of the class, but inside super() this is not allowed and neither anywhere before the parentclass constructor invocation.

How can I get the child class name to use as an argument inside super()?

Dirk J. Faber
  • 4,360
  • 5
  • 20
  • 58
  • 2
    What is the use of the childClass name ? In Js you can declare several classes with the same name, you wouldn't know which one is your instance using. you better rely on using `instanceof` than doing check on "class names". – Pierre Emmanuel Lallemant Aug 01 '19 at 08:53
  • I am using it for a number of properties (strings) that are defined by the childClass name and are needed in several places. – Dirk J. Faber Aug 01 '19 at 08:57
  • @Pierre Emmanuel Lallemant, several classes with the same name? Can you show an example? – lucifer63 Aug 01 '19 at 08:58
  • for example you copy ChildClass.js in ChildClass2.js without changing the code. Now `var Class1 = require('childClass.js'); var Class2 = require('childClass2.js')`. You can create instances with `new Class1()` and `new Class2()`, if you rely on `myobj.className` you will think they are using the same class. Using `myobj instanceof Class1` will work only with myobj is created with Class1. – Pierre Emmanuel Lallemant Aug 01 '19 at 09:10
  • @PierreEmmanuelLallemant, oh, sorry, i thought you mean that in JS you really can declare several classes with the same name, and it is not related to the current question. Ok, thanks for explanation. @DirkJ.Faber, what's the use of user defined className? Why can't you rely solely on `this.constructor.name`? – lucifer63 Aug 01 '19 at 10:38

1 Answers1

2

One option is to put the functionality into the parent constructor - if no className is passed, check this.constructor.name:

class ParentClass {
    constructor(className) {
        if (!className) {
            className = this.constructor.name;
        }
        this.className = className;
        this.classNameLowerCase = className.toLowerCase();
    }
}

class ChildClass extends ParentClass {
    constructor() {
        super();
    }
}

const c = new ChildClass();
console.log(c.className);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320