1

I'm wondering what is the difference between an anonymous function:

callback = function (a) {return a}

and using the "=>" notation?

callback = (a) => {return a}

Is it anything more than syntactic sugar?

eran
  • 14,496
  • 34
  • 98
  • 144
  • Arrow head used as a syntax in many languages to separate the function parameters and the function implementation – Jude Niroshan Nov 08 '16 at 08:41
  • 1
    The different between the two is that the second one captures "this" context while the other one takes the context of the caller function. So you can call the second function without .bind(this), and still the function will be able to use this.myvar – Aus Nov 08 '16 at 08:42
  • https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/ – 0aslam0 Nov 08 '16 at 08:45

3 Answers3

5

It is called ES6 fat arrow syntax. There is difference between them, fat arrow one automatically captured this.

Niyoko
  • 7,512
  • 4
  • 32
  • 59
2

() => is called arrow function of Javascript, which is introduced in ECMA Script 6. It's useful for more intuitive handling of current object context.

Reference link for new features of ECMA Script 6.

Harish Kommuri
  • 2,825
  • 1
  • 22
  • 28
-1

This has nothing to do with Node.js. Node.js is just a library, libraries can't introduce syntax in ECMAScript.

It's just a standard ECMAScript arrow function.

Is it anything more than syntactic sugar?

Yes. Arrow functions have lexically scoped this.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653