-1

Today I found a issue in my code with arrow functions that drove me completely crazy.

I thought that this:

 classMethod = (param1, param2) =>
    this.APImethod(param1, param2);

was equivalent to this:

classMethod = (param1, param2) =>
    {this.APImethod(param1, param2)}
Daniel A
  • 17
  • 1
  • 1
    FWIW, arrow functions in Java, Swift etc. behave the same - `x->foo()` means `x->{return foo()}` (Java syntax). It's fairly standard and comes from the Haskell family of languages which was inspired by one of the syntax for functions in mathematics – slebetman Mar 05 '20 at 14:49
  • Oh thanks. I wasn't aware that other languages have the same behavior. Kind of make sense. I'm only fluent with JavaScript. – Daniel A Mar 05 '20 at 16:08

1 Answers1

0

But no. This:

 classMethod = (param1, param2) =>
    this.APImethod(param1, param2);

is omitting a return statement. The function is returning "this.APImethod(param1,param2)", meanwhile

classMethod = (param1, param2) =>
    {this.APImethod(param1, param2)}

Doesn't return anything, because if you type the { }, you need to specify the return statement. The equivalent function would be:

classMethod = (param1, param2) =>
    {return this.APImethod(param1, param2)}

Hope this is useful for somebody.

Daniel A
  • 17
  • 1