0

This is similiar code construction in both cases of switch except word sendPhoto/sendVideo. How I can to change it, simplify.

class Telegram {
    defineMedia = 'photo';
    static sendPost(ctx) {
        switch (this.defineMedia){
            case 'photo':
                this.getUrl((urlCallback) => {
                    ctx.telegram.sendPhoto/*sendPhoto*/(this.#chatId,
                        {url: urlCallback});
                });
                break;
            case 'video':
                this.getUrl((urlCallback) => {
                    ctx.telegram.sendVideo/*sendVideo*/(this.#chatId,
                        {url: urlCallback});
                });
                break;
        }
    }
}

Telegram.sendPost();

How can I to change a switch to something like this:

    this.getUrl((urlCallback) => {
        ctx.telegram.(this.defineMedia = 'photo' ? sendPhoto : sendVideo )(this.#chatId,
            {url: urlCallback});
    });
Aalexander
  • 4,987
  • 3
  • 11
  • 34
subUser
  • 77
  • 1
  • 9
  • 1
    [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+js+call+method+name+from+variable) of [How to execute a JavaScript function when I have its name as a string](https://stackoverflow.com/q/359788/4642212). – Sebastian Simon Jan 25 '21 at 18:38
  • 1
    The class can be shortened to ``class Telegram { defineMedia = "photo"; static sendPost(ctx){ this.getUrl((urlCallback) => { ctx.telegram[`send${this.defineMedia.toUpperCase().slice(0, 1)}${this.defineMedia.slice(1)}`](this.#chatId, { url: urlCallback }); }); } }``. – Sebastian Simon Jan 25 '21 at 18:43

0 Answers0