[英]Arrow function in nodeJs
我在理解箭头功能时遇到问题,我知道箭头功能的键入方式如下()=>,但我想知道我们在其中键入箭头功能的功能如何工作
喜欢
app.listen(3000 , () => console.log('foo'));
我想知道热听功能调用箭头功能吗? 以及如何调用没有任何名称的箭头功能
那么,如果我想创建一个将箭头函数作为参数的函数,我该怎么做?
这称为回调函数,请查看MDN以获取文档: https : //developer.mozilla.org/zh-CN/docs/Glossary/Callback_function
该函数在父函数的参数中命名。
function myFunc(callbackFunc) {
//do stuff!
console.log("in parent func");
callbackFunc(); //calls the callback function passed as a param
console.log("Callback done!"); //If there is async code in your callback function, this may happen BEFORE the callbackFunc() is finished. A common gotcha to watch out for.
}
myFunc(() => { console.log("Doing the callback") });
这是ExpressJS使用回调函数的方式: https : //expressjs.com/en/guide/using-middleware.html
除了功能和箭头功能之间的重要区别外,它们非常相似。
无名:
app.listen(3000 , () => console.log('foo'));
// or
app.listen(3000 , function() { console.log('foo') } );
命名为:
function aaa() { ... }
// or
const aaa = () => { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.