javascript - Dynamically calling a static method - Stack Overflow

In a class there are several static methods and the method to be called will be decided on the run time

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 error TypeError: Class constructor channel cannot be invoked without 'new' – Suhail Gupta Commented Feb 16, 2017 at 18:12
Add a ment  | 

3 Answers 3

Reset to default 5

You 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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744184610a4562146.html

相关推荐

  • javascript - Dynamically calling a static method - Stack Overflow

    In a class there are several static methods and the method to be called will be decided on the run time

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信