• everybody knows that arrow functions inside methods ...

    From luserdroog@21:1/5 to All on Thu Mar 24 21:57:24 2022
    everybody knows that arrow functions inside methods forget their 'this'.
    Does everybody else know how to work around that? Does `bind()` do
    something helpful?

    --
    lazy amateur

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Fri Mar 25 09:19:11 2022
    luserdroog:

    everybody knows that arrow functions inside methods forget their 'this'.
    Does everybody else know how to work around that? Does `bind()` do
    something helpful?

    <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions>


    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Elhwen Dico@21:1/5 to All on Fri Mar 25 15:47:24 2022
    Le 25/03/2022 à 05:57, luserdroog a écrit :
    everybody knows that arrow functions inside methods forget their 'this'.
    Does everybody else know how to work around that? Does `bind()` do
    something helpful?


    Don't know what you mean...

    class Test {

    constructor(name) {
    this.name = name;
    }

    getName(delay) {
    setTimeout(
    () => {
    console.log(this.name);
    }, delay);
    }
    }

    const t1 = new Test("test 1");
    const t2 = new Test("test 2");

    t1.getName(500);
    t2.getName(100);

    test 2
    test 1

    anonymous arrow function inherits this from the function where it is
    defined (the getName method) whose this is the test object.
    of course

    const f = t1.getName;
    f(100);

    D:\xxxxxxxxxxxxxxxxxxxxxxx\test.js:10
    console.log(this.name);
    TypeError: Cannot read properties of undefined (reading 'name')

    the call to method getName via f(100) make this to be undefined so is
    this for arrow function

    and

    const f = t1.getName.bind(t1);
    f(100);

    test 1

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Haufe (TNO)@21:1/5 to luser...@gmail.com on Fri Mar 25 09:32:05 2022
    On Thursday, March 24, 2022 at 11:57:28 PM UTC-5, luser...@gmail.com wrote:
    everybody knows that arrow functions inside methods forget their 'this'.
    Does everybody else know how to work around that? Does `bind()` do
    something helpful?

    You use a function instead of a lambda. This is by design.

    The alternative is to pass 'this' as an explicit argument:

    (self, foo) => {...}

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From JJ@21:1/5 to luserdroog on Sat Mar 26 11:29:09 2022
    On Thu, 24 Mar 2022 21:57:24 -0700 (PDT), luserdroog wrote:
    everybody knows that arrow functions inside methods forget their 'this'.
    Does everybody else know how to work around that? Does `bind()` do
    something helpful?

    Arrow function is not a replacement or a "better" version of the normal function. Both have different purposes.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)