0

I have the following code:

const myObj = {
        reply(text: string, options?: Bot.SendMessageOptions)
        {
            return bot.sendMessage(msg.chat.id, text, { reply_to_message_id: msg.message_id, ...options });
        },
        replyMd(text: string, options?: Bot.SendMessageOptions)
        {
            return this.reply(text, { parse_mode: "Markdown" });
        }
};

However, when I call replyMd, then this is undefined. Why?

laptou
  • 4,262
  • 1
  • 18
  • 33

1 Answers1

0

Consider this example:

const foo = {
  bar() {
    console.log(this)
  }
}

Calling foo.bar() logs

Object {bar: function bar()}

but

const {bar} = foo
bar()

logs undefined

See this MDN article for a detailed explanation.

Inside a function, the value of this depends on how the function is called. [...] When a function is called as a method of an object, its this is set to the object the method is called on.

Stefan
  • 675
  • 3
  • 9