如何根据字符串值实例化对象
How can I instantiate an object based on a string value
我有一个字符串,我想在 API 工厂中使用它来实例化来自 class 的正确对象。这是代码:
import StoryApiService from './story'
import AssignmentApiService from './assignment'
let apiTypes = {
story: null,
assignment: null
}
let token
const getApi = (newToken, apiType = 'story') => {
const isNewToken = newToken => newToken !== token
const shouldCreateService = !apiTypes[apiType] || isNewToken
if( shouldCreateService ) {
const capitalizedServiceType = apiType.charAt(0).toUpperCase() + apiType.slice(1)
// this line is what I need help with
apiTypes[apiType] = new `${capitalizedServiceType}ApiService`(token)
}
return apiTypes[apiType]
}
所以基本上取决于传入的 apiType
参数,我想从正确的 class 实例化一个新对象。如果可能的话,我想避免使用 if/else
和 switch
语句,因为我有一堆不同的 apiServices,我认为如果可能的话,这种方式会更干净。
我知道上面代码中的行不会像写的那样工作,但它的伪代码显示了我想要达到的效果。
做与 apiTypes[apiType]
相同的事情:访问包含 class/constructor 函数的对象。
例如如果它是您在 window
范围内定义的 class:
const ObjectType = window[`${capitalizedServiceType}ApiService`];
然后记得验证是否已定义,因为您无法保证您的字符串实际映射到函数或class:
if (ObjectType) {
apiTypes[apiType] = new ObjectType(token);
} else {
console.error(`Api service for "${capitalizedServiceType}" does not exist.`);
}
与其尝试从字符串名称实例化 class(使用一些复杂的 capitalization/concatenation 逻辑),不如创建一个将 apiType
名称直接映射到其对应的 [=15] 的对象=]es:
import StoryApiService from './story'
import AssignmentApiService from './assignment'
const services = {
story: StoryApiService,
assignment: AssignmentApiService,
}
const serviceInstances = {
story: null,
assignment: null,
}
let token
const getApi = (newToken, apiType = 'story') => {
const isNewToken = newToken !== token
const shouldCreateService = !serviceInstances[apiType] || isNewToken
if (shouldCreateService) {
token = newToken
serviceInstances[apiType] = new services[apiType](token)
}
return serviceInstances[apiType]
}
我有一个字符串,我想在 API 工厂中使用它来实例化来自 class 的正确对象。这是代码:
import StoryApiService from './story'
import AssignmentApiService from './assignment'
let apiTypes = {
story: null,
assignment: null
}
let token
const getApi = (newToken, apiType = 'story') => {
const isNewToken = newToken => newToken !== token
const shouldCreateService = !apiTypes[apiType] || isNewToken
if( shouldCreateService ) {
const capitalizedServiceType = apiType.charAt(0).toUpperCase() + apiType.slice(1)
// this line is what I need help with
apiTypes[apiType] = new `${capitalizedServiceType}ApiService`(token)
}
return apiTypes[apiType]
}
所以基本上取决于传入的 apiType
参数,我想从正确的 class 实例化一个新对象。如果可能的话,我想避免使用 if/else
和 switch
语句,因为我有一堆不同的 apiServices,我认为如果可能的话,这种方式会更干净。
我知道上面代码中的行不会像写的那样工作,但它的伪代码显示了我想要达到的效果。
做与 apiTypes[apiType]
相同的事情:访问包含 class/constructor 函数的对象。
例如如果它是您在 window
范围内定义的 class:
const ObjectType = window[`${capitalizedServiceType}ApiService`];
然后记得验证是否已定义,因为您无法保证您的字符串实际映射到函数或class:
if (ObjectType) {
apiTypes[apiType] = new ObjectType(token);
} else {
console.error(`Api service for "${capitalizedServiceType}" does not exist.`);
}
与其尝试从字符串名称实例化 class(使用一些复杂的 capitalization/concatenation 逻辑),不如创建一个将 apiType
名称直接映射到其对应的 [=15] 的对象=]es:
import StoryApiService from './story'
import AssignmentApiService from './assignment'
const services = {
story: StoryApiService,
assignment: AssignmentApiService,
}
const serviceInstances = {
story: null,
assignment: null,
}
let token
const getApi = (newToken, apiType = 'story') => {
const isNewToken = newToken !== token
const shouldCreateService = !serviceInstances[apiType] || isNewToken
if (shouldCreateService) {
token = newToken
serviceInstances[apiType] = new services[apiType](token)
}
return serviceInstances[apiType]
}