最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically calling a static method - Stack Overflow

programmeradmin4浏览0评论

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)

发布评论

评论列表(0)

  1. 暂无评论