Referencing `this` in an object literal [duplicate]












0
















This question already has an answer here:




  • How does the “this” keyword work?

    22 answers




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?










share|improve this question













marked as duplicate by Andreas, Nicholas Tower, laptou, Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    how are you calling replyMd?

    – Nicholas Tower
    Nov 26 '18 at 17:28











  • Where you called replyMd method?

    – Jin
    Nov 26 '18 at 17:29











  • @NicholasTower const { replyMd } = myObj; replyMd("someText", { someOptions: true });

    – laptou
    Nov 26 '18 at 17:30








  • 2





    const { replyMd } = myObj; replyMd("someText", { someOptions: true }); That will invoke it with no context, so this is set to the window object (in non-strict mode) or undefined (in strict mode).

    – Nicholas Tower
    Nov 26 '18 at 17:36
















0
















This question already has an answer here:




  • How does the “this” keyword work?

    22 answers




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?










share|improve this question













marked as duplicate by Andreas, Nicholas Tower, laptou, Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    how are you calling replyMd?

    – Nicholas Tower
    Nov 26 '18 at 17:28











  • Where you called replyMd method?

    – Jin
    Nov 26 '18 at 17:29











  • @NicholasTower const { replyMd } = myObj; replyMd("someText", { someOptions: true });

    – laptou
    Nov 26 '18 at 17:30








  • 2





    const { replyMd } = myObj; replyMd("someText", { someOptions: true }); That will invoke it with no context, so this is set to the window object (in non-strict mode) or undefined (in strict mode).

    – Nicholas Tower
    Nov 26 '18 at 17:36














0












0








0









This question already has an answer here:




  • How does the “this” keyword work?

    22 answers




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?










share|improve this question















This question already has an answer here:




  • How does the “this” keyword work?

    22 answers




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?





This question already has an answer here:




  • How does the “this” keyword work?

    22 answers








javascript typescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 17:27









laptoulaptou

1,8191021




1,8191021




marked as duplicate by Andreas, Nicholas Tower, laptou, Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Andreas, Nicholas Tower, laptou, Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 18:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    how are you calling replyMd?

    – Nicholas Tower
    Nov 26 '18 at 17:28











  • Where you called replyMd method?

    – Jin
    Nov 26 '18 at 17:29











  • @NicholasTower const { replyMd } = myObj; replyMd("someText", { someOptions: true });

    – laptou
    Nov 26 '18 at 17:30








  • 2





    const { replyMd } = myObj; replyMd("someText", { someOptions: true }); That will invoke it with no context, so this is set to the window object (in non-strict mode) or undefined (in strict mode).

    – Nicholas Tower
    Nov 26 '18 at 17:36














  • 1





    how are you calling replyMd?

    – Nicholas Tower
    Nov 26 '18 at 17:28











  • Where you called replyMd method?

    – Jin
    Nov 26 '18 at 17:29











  • @NicholasTower const { replyMd } = myObj; replyMd("someText", { someOptions: true });

    – laptou
    Nov 26 '18 at 17:30








  • 2





    const { replyMd } = myObj; replyMd("someText", { someOptions: true }); That will invoke it with no context, so this is set to the window object (in non-strict mode) or undefined (in strict mode).

    – Nicholas Tower
    Nov 26 '18 at 17:36








1




1





how are you calling replyMd?

– Nicholas Tower
Nov 26 '18 at 17:28





how are you calling replyMd?

– Nicholas Tower
Nov 26 '18 at 17:28













Where you called replyMd method?

– Jin
Nov 26 '18 at 17:29





Where you called replyMd method?

– Jin
Nov 26 '18 at 17:29













@NicholasTower const { replyMd } = myObj; replyMd("someText", { someOptions: true });

– laptou
Nov 26 '18 at 17:30







@NicholasTower const { replyMd } = myObj; replyMd("someText", { someOptions: true });

– laptou
Nov 26 '18 at 17:30






2




2





const { replyMd } = myObj; replyMd("someText", { someOptions: true }); That will invoke it with no context, so this is set to the window object (in non-strict mode) or undefined (in strict mode).

– Nicholas Tower
Nov 26 '18 at 17:36





const { replyMd } = myObj; replyMd("someText", { someOptions: true }); That will invoke it with no context, so this is set to the window object (in non-strict mode) or undefined (in strict mode).

– Nicholas Tower
Nov 26 '18 at 17:36












1 Answer
1






active

oldest

votes


















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.







share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.







    share|improve this answer




























      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.







      share|improve this answer


























        0












        0








        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.







        share|improve this answer













        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.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 17:44









        StefanStefan

        60028




        60028

















            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)