0

I have some classes I want to extend and I want the new classes to have their parent class's name with a prefix. I want to do this in a single place in a single function.

Pseudo-code:

function getChildClass(parentConstructor) {
    return class `ChildOf${parentConstructor.name}` {
        constructor() { /*some common logic*/ }
    }
}

This is a syntax error, however.

This is purely for aesthetic reasons. I just want the names to reflect the parent class they extend when they show up during debugging.

Kevin Beal
  • 10,500
  • 12
  • 66
  • 92

1 Answers1

0

let libs = {}
function init(constructorName) {
 libs[constructorName] = function () { return this}
 return libs
}
init('canvasInstruction')
new libs.canvasInstrucion()

You have to define what that function is and what it does. But as long as it returns a class or a function returning this, you can have flexible constructors.

Bhavesh Ajani
  • 991
  • 9
  • 11