Referencing `this` in an object literal [duplicate]
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?
javascript typescript
marked as duplicate by Andreas, Nicholas Tower, laptou, Bergi
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.
add a comment |
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?
javascript typescript
marked as duplicate by Andreas, Nicholas Tower, laptou, Bergi
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
@NicholasTowerconst { 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, sothis
is set to the window object (in non-strict mode) orundefined
(in strict mode).
– Nicholas Tower
Nov 26 '18 at 17:36
add a comment |
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?
javascript typescript
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
javascript typescript
asked Nov 26 '18 at 17:27
laptoulaptou
1,8191021
1,8191021
marked as duplicate by Andreas, Nicholas Tower, laptou, Bergi
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
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
@NicholasTowerconst { 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, sothis
is set to the window object (in non-strict mode) orundefined
(in strict mode).
– Nicholas Tower
Nov 26 '18 at 17:36
add a comment |
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
@NicholasTowerconst { 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, sothis
is set to the window object (in non-strict mode) orundefined
(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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 26 '18 at 17:44
StefanStefan
60028
60028
add a comment |
add a comment |
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, sothis
is set to the window object (in non-strict mode) orundefined
(in strict mode).– Nicholas Tower
Nov 26 '18 at 17:36