Search code examples
javascriptclassecmascript-6

ES6 Dynamic class names


I have been experimenting with ES6 classes and am wondering if you can change class names dynamically? For example

class [Some dynamic name] {}; 

Solution

  • There is probably a better solution for whatever you are trying to achieve, but you can assign a class expression to an object:

    let classes = {};
    classes[someName] = class { ... };
    

    This didn't really change in ES2015: if you want to create a dynamically named binding, you have to use an object or some other mapping instead.