0

I have been reading articles about Node.js and MQTT. And I am strange to the syntax as:

client.on('connect', () => {
  // do something
})

the () => {} thing, is it like function() {} ready for callback? Or does it have some specific usage? Is there some reference about that?

hardillb
  • 41,053
  • 9
  • 49
  • 77
krave
  • 1,059
  • 2
  • 11
  • 26

2 Answers2

1

It's a shorthand for

client.on('connect', function() {
    // do something
});
user3427419
  • 1,540
  • 8
  • 13
  • 1
    There is more to arrow functions than just being a 'shorthand'. You neglected to mention its primary feature. – Marty Jan 13 '16 at 04:14
0

That is the ES6 arrow function syntax. It is similar to function() {}, along with lexically binding this.

(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression
         // equivalent to:  => { return expression; }

// Parentheses are optional when there's only one parameter:
(singleParam) => { statements }
singleParam => { statements }

// A function with no parameters requires parentheses:
() => { statements }
jumbopap
  • 3,554
  • 2
  • 22
  • 42