0

How to replace XXX so that the replacement will be evaluated for the correspondent class that get imported?e.g. if name is "FooService", then the imported FooService class will be evaluated and used in XXX.

Class FooService:

export class FooService {}

Class BarService:

export class BarService {}

Class Container:

import {FooService} from './FooService'
import {BarService} from './BarService'

export class Container {
    constructor() {
        this._services = new Map();
    }
    getServiceByName(name) {
        if (!this._services.has(name)) {
         // How to replace XXX so that the replacement will 
         // be evaluated for the correspondent class that get imported?
         // e.g. if name is "FooService", then the imported FooService 
         // class will be evaluated and used in XXX.
           this._services.set(name, XXX); 
        }

        return this._service.get(name);
    }
}
Downhillski
  • 2,555
  • 2
  • 27
  • 39
  • 1
    Why do you need to do this dynamically? Just initialize the map with all the names and classes. – Ry- May 24 '17 at 01:29
  • @Ryan, good point! – Downhillski May 24 '17 at 01:33
  • Possible duplicate of [Create an instance of a class in ES6 with a dynamic name?](https://stackoverflow.com/questions/34655616/create-an-instance-of-a-class-in-es6-with-a-dynamic-name) – le_m May 24 '17 at 02:07
  • IMHO it is not possible to access a class (= constructor function) from a string containing its name apart from dirty `eval(name)` – le_m May 24 '17 at 02:09

0 Answers0