Javascript 如何通过提供类名字符串文字来获取类?
如何替换XXX,以便为导入的对应类评估替换?例如,如果名称为“FooService”,则导入的FooService类将在XXX中评估和使用 食品类服务:Javascript 如何通过提供类名字符串文字来获取类?,javascript,Javascript,如何替换XXX,以便为导入的对应类评估替换?例如,如果名称为“FooService”,则导入的FooService类将在XXX中评估和使用 食品类服务: export class FooService {} export class BarService {} import {FooService} from './FooService' import {BarService} from './BarService' export class Container { constru
export class FooService {}
export class BarService {}
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);
}
}
分类酒吧服务:
export class FooService {}
export class BarService {}
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);
}
}
类容器:
export class FooService {}
export class BarService {}
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);
}
}
为什么需要动态地执行此操作?只要用所有的名称和类初始化地图。@Ryan,说得好!IMHO的可能重复除了dirty
eval(name)