Create object from class name in JavasScript ECMAScript 6

前端 未结 6 1606
感动是毒
感动是毒 2020-11-22 15:46

I want create object factory using ES6 but old-style syntax doesn\'t work with new.

I have next code:

export class Column {}
export class Sequence {}         


        
6条回答
  •  有刺的猬
    2020-11-22 16:02

    I prefer this method:

    allThemClasses.js

    export class A {}
    export class B {}
    export class C {}
    

    script.js

    import * as Classes from './allThemClasses';
    
    const a = new Classes['A'];
    const b = new Classes['B'];
    const c = new Classes['C'];
    

提交回复
热议问题