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
  • 47,764
  • 9
  • 59
  • 93
krave
  • 1,305
  • 2
  • 13
  • 30

2 Answers2

1

It's a shorthand for

client.on('connect', function() {
    // do something
});
user3427419
  • 1,720
  • 10
  • 14
  • 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,734
  • 2
  • 23
  • 45