【发布时间】:2017-02-16 18:08:29
【问题描述】:
在一个类中有几个static 方法,调用的方法将在运行时决定。如何动态调用此方法?
export default class channel {
// METHOD THAT WILL DYNAMICALLY CALL OTHER STATIC METHODS
private static methodMap = {
'channel-create' : 'create',
'channel-user-count' : 'userCount',
'channel-close' : 'close'
};
public static do(commandType:string,params: any) {
if(channel.methodMap.hasOwnProperty(commandType)) {
// GET NAME OF THE METHOD
let method = channel.methodMap[commandType];
// CALL METHOD ON THE FLY
//return channel.call(method,params);
// channel.userCount(params);
}
}
/**
* Adds channel to available channel list
*/
private static create(channelName:string) {
}
/**
* Returns count of users in the channel
*/
private static userCount(channelName:string) {
}
}
【问题讨论】:
-
你的解决方案有什么问题?
-
@nrabinowitz 我没有。
call方法抛出错误TypeError: Class constructor channel cannot be invoked without 'new'
标签: javascript typescript static