node.js中的回调函数
Functions are the very basic and fundamental topic of any programming language. Understanding them in great detail is very much necessary for any programmer or coder.
函数是任何编程语言中非常基本的主题。 对于任何程序员或编码人员而言,非常详细地了解它们都是非常必要的。
Thus, in this article, I will throw light on anonymous functions in JavaScript.
因此,在本文中,我将介绍JavaScript中的匿名函数。
They are a very strong tool for generating concise codes and are very commonly seen in JavaScript libraries.
它们是生成简明代码的强大工具,在JavaScript库中很常见。
Example:
例:
(function(food) {
if (food === "cookies") {
console.log("More please");
} else if (food === "cake") {
console.log("Yum yum");
}
})("cookies");
Output
输出量
![anonymous function output | Node JS](https://www.includehelp.com/node-js/Images/anonymous-function-output.jpg)
Explanation:
说明:
It might seem to be very confusing on first glance, but it is not so.
乍一看似乎很令人困惑,但事实并非如此。
In line number 7 we are just calling a function and passing cookies as an argument to it. But the function we are calling is anonymous i.e. it does not have a name.
在第7行中,我们只是调用一个函数并将cookie作为参数传递给它。 但是我们正在调用的函数是匿名的,即它没有名称。
So instead of using a function reference we are defining the function or using a function expression.
因此,我们不用定义函数引用,而是定义函数或使用函数表达式。
Also, pay attention that extra set of parenthesis is very much necessary for these kinds of function calling otherwise java interpreter won't be able to identify it as a function.
另外,请注意,对于此类函数调用,非常需要额外的括号集,否则java解释器将无法将其识别为函数。
翻译自: https://www.includehelp.com/node-js/anonymous-function.aspx
node.js中的回调函数