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;








1
















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?










share|improve this question















marked as duplicate by Felix Kling ecmascript-6
Users with the  ecmascript-6 badge can single-handedly close ecmascript-6 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 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 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





    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


















1
















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?










share|improve this question















marked as duplicate by Felix Kling ecmascript-6
Users with the  ecmascript-6 badge can single-handedly close ecmascript-6 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 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 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





    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














1












1








1









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?










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 ecmascript-6
Users with the  ecmascript-6 badge can single-handedly close ecmascript-6 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 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 ecmascript-6
Users with the  ecmascript-6 badge can single-handedly close ecmascript-6 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 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 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





    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


















  • 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





    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

















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













1 Answer
1






active

oldest

votes


















0














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());





share|improve this answer































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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());





    share|improve this answer





























      0














      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());





      share|improve this answer



























        0












        0








        0







        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());





        share|improve this answer















        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());






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 15:21

























        answered Nov 16 '18 at 14:58









        squeekyDavesqueekyDave

        489217




        489217















            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党