javascript function within an object is undefined [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Methods in ES6 objects: using arrow functions
4 answers
Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?
3 answers
const md =
methodOne: () =>
console.log(this.anotherMethod())
,
anotherMethod: () => 'anotherMethod'
When I do md.methodOne()
I got anotherMethod
is undefined error. How do I call anotherMethod
in methodOne
?
javascript ecmascript-6
marked as duplicate by Felix Kling
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 16 '18 at 18:36
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:
Methods in ES6 objects: using arrow functions
4 answers
Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?
3 answers
const md =
methodOne: () =>
console.log(this.anotherMethod())
,
anotherMethod: () => 'anotherMethod'
When I do md.methodOne()
I got anotherMethod
is undefined error. How do I call anotherMethod
in methodOne
?
javascript ecmascript-6
marked as duplicate by Felix Kling
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 16 '18 at 18:36
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.
Arrow functions do not setthis
, sothis.anotherMethod
cannot be resolved. If you definemethodOne
with afunction
declaration, the code will work.
– Pointy
Nov 16 '18 at 14:26
2
makes no sense toconsole.log(this.anotherMethod())
since anotherMethod does not return anything
– epascarello
Nov 16 '18 at 14:26
@epascarello edited my question, how about now? this isgone
if I use arrow function, right?
– alice tang
Nov 16 '18 at 14:53
add a comment |
This question already has an answer here:
Methods in ES6 objects: using arrow functions
4 answers
Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?
3 answers
const md =
methodOne: () =>
console.log(this.anotherMethod())
,
anotherMethod: () => 'anotherMethod'
When I do md.methodOne()
I got anotherMethod
is undefined error. How do I call anotherMethod
in methodOne
?
javascript ecmascript-6
This question already has an answer here:
Methods in ES6 objects: using arrow functions
4 answers
Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?
3 answers
const md =
methodOne: () =>
console.log(this.anotherMethod())
,
anotherMethod: () => 'anotherMethod'
When I do md.methodOne()
I got anotherMethod
is undefined error. How do I call anotherMethod
in methodOne
?
This question already has an answer here:
Methods in ES6 objects: using arrow functions
4 answers
Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?
3 answers
javascript ecmascript-6
javascript ecmascript-6
edited Nov 16 '18 at 15:16
alice tang
asked Nov 16 '18 at 14:25
alice tangalice tang
133
133
marked as duplicate by Felix Kling
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 16 '18 at 18:36
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 Felix Kling
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 16 '18 at 18:36
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.
Arrow functions do not setthis
, sothis.anotherMethod
cannot be resolved. If you definemethodOne
with afunction
declaration, the code will work.
– Pointy
Nov 16 '18 at 14:26
2
makes no sense toconsole.log(this.anotherMethod())
since anotherMethod does not return anything
– epascarello
Nov 16 '18 at 14:26
@epascarello edited my question, how about now? this isgone
if I use arrow function, right?
– alice tang
Nov 16 '18 at 14:53
add a comment |
Arrow functions do not setthis
, sothis.anotherMethod
cannot be resolved. If you definemethodOne
with afunction
declaration, the code will work.
– Pointy
Nov 16 '18 at 14:26
2
makes no sense toconsole.log(this.anotherMethod())
since anotherMethod does not return anything
– epascarello
Nov 16 '18 at 14:26
@epascarello edited my question, how about now? this isgone
if I use arrow function, right?
– alice tang
Nov 16 '18 at 14:53
Arrow functions do not set
this
, so this.anotherMethod
cannot be resolved. If you define methodOne
with a function
declaration, the code will work.– Pointy
Nov 16 '18 at 14:26
Arrow functions do not set
this
, so this.anotherMethod
cannot be resolved. If you define methodOne
with a function
declaration, the code will work.– Pointy
Nov 16 '18 at 14:26
2
2
makes no sense to
console.log(this.anotherMethod())
since anotherMethod does not return anything– epascarello
Nov 16 '18 at 14:26
makes no sense to
console.log(this.anotherMethod())
since anotherMethod does not return anything– epascarello
Nov 16 '18 at 14:26
@epascarello edited my question, how about now? this is
gone
if I use arrow function, right?– alice tang
Nov 16 '18 at 14:53
@epascarello edited my question, how about now? this is
gone
if I use arrow function, right?– alice tang
Nov 16 '18 at 14:53
add a comment |
1 Answer
1
active
oldest
votes
The problem is in your arrow functions.
An arrow function does not have its own this
; the this
value of the enclosing lexical context is used i.e. Arrow functions follow the normal variable lookup rules. So while searching for this
which is not present in current scope they end up finding this
from its enclosing scope . Thus, in the following code, the this
within the function that is passed to setInterval has the same value as this
in the lexically enclosing function:
Also,
you don't need to type return
when you have :
() => return 'some message'
The arrow function will return implicitly the value or the result of an expression:
const msg = () => 'some message'
const result = () => 5 * 5; //25
Just for the sake of it, this is how it should look:
const md =
methodOne:function ()
console.log(this.anotherMethod());
,
anotherMethod: function ()
return 'anotherMethod';
console.log(md.methodOne());
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is in your arrow functions.
An arrow function does not have its own this
; the this
value of the enclosing lexical context is used i.e. Arrow functions follow the normal variable lookup rules. So while searching for this
which is not present in current scope they end up finding this
from its enclosing scope . Thus, in the following code, the this
within the function that is passed to setInterval has the same value as this
in the lexically enclosing function:
Also,
you don't need to type return
when you have :
() => return 'some message'
The arrow function will return implicitly the value or the result of an expression:
const msg = () => 'some message'
const result = () => 5 * 5; //25
Just for the sake of it, this is how it should look:
const md =
methodOne:function ()
console.log(this.anotherMethod());
,
anotherMethod: function ()
return 'anotherMethod';
console.log(md.methodOne());
add a comment |
The problem is in your arrow functions.
An arrow function does not have its own this
; the this
value of the enclosing lexical context is used i.e. Arrow functions follow the normal variable lookup rules. So while searching for this
which is not present in current scope they end up finding this
from its enclosing scope . Thus, in the following code, the this
within the function that is passed to setInterval has the same value as this
in the lexically enclosing function:
Also,
you don't need to type return
when you have :
() => return 'some message'
The arrow function will return implicitly the value or the result of an expression:
const msg = () => 'some message'
const result = () => 5 * 5; //25
Just for the sake of it, this is how it should look:
const md =
methodOne:function ()
console.log(this.anotherMethod());
,
anotherMethod: function ()
return 'anotherMethod';
console.log(md.methodOne());
add a comment |
The problem is in your arrow functions.
An arrow function does not have its own this
; the this
value of the enclosing lexical context is used i.e. Arrow functions follow the normal variable lookup rules. So while searching for this
which is not present in current scope they end up finding this
from its enclosing scope . Thus, in the following code, the this
within the function that is passed to setInterval has the same value as this
in the lexically enclosing function:
Also,
you don't need to type return
when you have :
() => return 'some message'
The arrow function will return implicitly the value or the result of an expression:
const msg = () => 'some message'
const result = () => 5 * 5; //25
Just for the sake of it, this is how it should look:
const md =
methodOne:function ()
console.log(this.anotherMethod());
,
anotherMethod: function ()
return 'anotherMethod';
console.log(md.methodOne());
The problem is in your arrow functions.
An arrow function does not have its own this
; the this
value of the enclosing lexical context is used i.e. Arrow functions follow the normal variable lookup rules. So while searching for this
which is not present in current scope they end up finding this
from its enclosing scope . Thus, in the following code, the this
within the function that is passed to setInterval has the same value as this
in the lexically enclosing function:
Also,
you don't need to type return
when you have :
() => return 'some message'
The arrow function will return implicitly the value or the result of an expression:
const msg = () => 'some message'
const result = () => 5 * 5; //25
Just for the sake of it, this is how it should look:
const md =
methodOne:function ()
console.log(this.anotherMethod());
,
anotherMethod: function ()
return 'anotherMethod';
console.log(md.methodOne());
edited Nov 16 '18 at 15:21
answered Nov 16 '18 at 14:58
squeekyDavesqueekyDave
489217
489217
add a comment |
add a comment |
Arrow functions do not set
this
, sothis.anotherMethod
cannot be resolved. If you definemethodOne
with afunction
declaration, the code will work.– Pointy
Nov 16 '18 at 14:26
2
makes no sense to
console.log(this.anotherMethod())
since anotherMethod does not return anything– epascarello
Nov 16 '18 at 14:26
@epascarello edited my question, how about now? this is
gone
if I use arrow function, right?– alice tang
Nov 16 '18 at 14:53