Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #40821 > unrolled thread

everybody knows that arrow functions inside methods ...

Started byluserdroog <luser.droog@gmail.com>
First post2022-03-24 21:57 -0700
Last post2022-03-26 11:29 +0700
Articles 5 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  everybody knows that arrow functions inside methods ... luserdroog <luser.droog@gmail.com> - 2022-03-24 21:57 -0700
    Re: everybody knows that arrow functions inside methods ... Arno Welzel <usenet@arnowelzel.de> - 2022-03-25 09:19 +0100
    Re: everybody knows that arrow functions inside methods ... Elhwen Dico <elhwen.dicote@gmail.com> - 2022-03-25 15:47 +0100
    Re: everybody knows that arrow functions inside methods ... "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2022-03-25 09:32 -0700
    Re: everybody knows that arrow functions inside methods ... JJ <jj4public@gmail.com> - 2022-03-26 11:29 +0700

#40821 — everybody knows that arrow functions inside methods ...

Fromluserdroog <luser.droog@gmail.com>
Date2022-03-24 21:57 -0700
Subjecteverybody knows that arrow functions inside methods ...
Message-ID<1e2d10ba-22e4-43fb-86b6-c0f016e7a991n@googlegroups.com>
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

[toc] | [next] | [standalone]


#40822

FromArno Welzel <usenet@arnowelzel.de>
Date2022-03-25 09:19 +0100
Message-ID<ja5c7vFe0m9U1@mid.individual.net>
In reply to#40821
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

[toc] | [prev] | [next] | [standalone]


#40824

FromElhwen Dico <elhwen.dicote@gmail.com>
Date2022-03-25 15:47 +0100
Message-ID<623dd5fc$0$25346$426a74cc@news.free.fr>
In reply to#40821
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

[toc] | [prev] | [next] | [standalone]


#40827

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2022-03-25 09:32 -0700
Message-ID<35b310ee-8472-4f91-9648-0e1810b821edn@googlegroups.com>
In reply to#40821
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) => {...}

[toc] | [prev] | [next] | [standalone]


#40829

FromJJ <jj4public@gmail.com>
Date2022-03-26 11:29 +0700
Message-ID<rjofcdzn7v8s$.xmvpoejbx47e$.dlg@40tude.net>
In reply to#40821
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web