0

What is the difference between assigning an anonymous arrow function to a variable (here constant):

const consumeError = (error)=> {
    console.error(`Aborted, reason: ${error}`);
};

And creating a named function:

function consumeError2(error) {
    console.error(`Aborted, reason: ${error}`);
}

I am don't see any difference in behavior. Are there any hidden implications regarding performance, memory leaks, coding style and so on?

Jan Grz
  • 1,373
  • 14
  • 18
  • 1
    lambdas don't have an (own) `this`- or `arguments`-object. – Thomas May 22 '16 at 15:35
  • 2
    @Thomas, you mean "arrow functions" not "lambdas". `function(x) { return x; }` is also a "lambda" and it does have its own `this` and `arguments`. – Mulan May 22 '16 at 15:57
  • Just looked it up, you're right. _I had learned that "lambda" is synonymous to "arrow function", but it actually refers to "anonymous function". You live and learn; thx @naomik_ – Thomas May 22 '16 at 16:43
  • 1
    I think the explanation **"don't have its own `this`"** is misleading. Arrows bind their `this` lexically instead of dynamically. Hence they have a `this` but it depends on the environment in which they were defined. –  May 22 '16 at 17:28
  • 1
    @Iven: Actually that they "don't have an own `this`" is technically more correct - [it's not bound to anything](http://stackoverflow.com/a/32539428/1048572). – Bergi May 22 '16 at 20:43
  • @Bergi I see, `this` is inherited from the lexical environment, thanks! –  May 22 '16 at 20:49

0 Answers0