语法()=>{}在Node.js中的平均值

what does the syntax () => {} mean in Node.js

本文关键字:Node js 平均值 gt 语法      更新时间:2023-09-26

我一直在阅读关于Node.js和MQTT的文章。我对以下语法感到奇怪:

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

() => {}的事情,它像function() {}一样准备好回调了吗?或者它有特定的用途吗?有什么参考资料吗?

这是的简写

client.on('connect', function() {
    // do something
});

这是ES6箭头函数语法。它类似于function() {},以及词汇结合的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 }