In a class there are several static
methods and the method to be called will be decided on the run time. How could I call this method dynamically?
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(mandType:string,params: any) {
if(channel.methodMap.hasOwnProperty(mandType)) {
// GET NAME OF THE METHOD
let method = channel.methodMap[mandType];
// 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) {
}
}
In a class there are several static
methods and the method to be called will be decided on the run time. How could I call this method dynamically?
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(mandType:string,params: any) {
if(channel.methodMap.hasOwnProperty(mandType)) {
// GET NAME OF THE METHOD
let method = channel.methodMap[mandType];
// 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) {
}
}
Share
Improve this question
edited Feb 16, 2017 at 18:13
Suhail Gupta
asked Feb 16, 2017 at 18:08
Suhail GuptaSuhail Gupta
23.3k67 gold badges213 silver badges344 bronze badges
2
- What's wrong with the solution you have? – nrabinowitz Commented Feb 16, 2017 at 18:10
-
@nrabinowitz I don't have one.
call
method throws an errorTypeError: Class constructor channel cannot be invoked without 'new'
– Suhail Gupta Commented Feb 16, 2017 at 18:12
3 Answers
Reset to default 5You can dynamically invoke a method by using Classname['methodName'](param)
. As in your case, you can invoke create
method as Channel['create']('MyChannel')
Here is the working example: Typescript Playground
class Channel {
private static methodMap = {
'channel-create' : 'create',
'channel-user-count' : 'userCount',
'channel-close' : 'close'
};
private static create(channelName:string) {
alert('Called with ' + channelName);
}
private static userCount(channelName:string) {
alert('Usercount called with ' + channelName);
}
public static do(mandType: string, params: any) {
if(Channel.methodMap.hasOwnProperty(mandType)) {
let method = Channel.methodMap[mandType];
return Channel[method](params);
}
}
}
Channel.do('channel-create', 'MyChannel');
Channel.do('channel-user-count', 1000);
Edit: Even though the above approach works, As @Ryan mentioned in his answer, providing functions directly in map is much cleaner.
private static methodMap: MethodMap = {
'channel-create': Channel.create,
'channel-user-count': Channel.userCount,
'channel-close': Channel.close,
};
Store the functions directly in the map:
type MethodMap = { [name: string]: (any) => void };
private static methodMap: MethodMap = {
'channel-create': Channel.create,
'channel-user-count': Channel.userCount,
'channel-close': Channel.close,
};
public static do(mandType: string, params: any) {
if (channel.methodMap.hasOwnProperty(mandType)) {
const method = channel.methodMap[mandType];
method(params);
}
}
To add to the answer by @HardikModha, you can also get the piler to check the mandType
against the possible values:
public static do(mandType: keyof typeof Channel.methodMap, params: any) {
if(Channel.methodMap.hasOwnProperty(mandType)) {
let method = Channel.methodMap[mandType];
return Channel[method](params);
}
}
...
Channel.do('channel-create', 'MyChannel'); // fine
Channel.do('channel-created', 'MyChannel'); // error
(code in playground)