ES6 Dynamic class names

前端 未结 6 1863
故里飘歌
故里飘歌 2020-11-27 17:45

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

class [Some dynamic name] {}; 

6条回答
  •  鱼传尺愫
    2020-11-27 18:17

    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.

提交回复
热议问题