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
  • 54,545
  • 11
  • 67
  • 105
krave
  • 1,629
  • 4
  • 17
  • 36

2 Answers2

1

It's a shorthand for

client.on('connect', function() {
    // do something
});
user3427419
  • 1,769
  • 11
  • 15
  • 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,969
  • 5
  • 27
  • 47