简体   繁体   English

node.js聊天应用程序中callback()的含义或源代码

[英]Meaning or source code for callback() in node.js chat app

I'm trying to go through the source code for the node chat demo seen here . 我正在尝试查看此处看到的节点聊天演示的源代码。 In the server.js file and fu.js file there is a function referenced as callback() seen here: 在server.js文件和fu.js文件中,存在一个称为callback()的函数,在此处可见:

function loadResponseData(callback) { 
if (body && headers && !DEBUG) { //if they already have value
  callback();
  return;
}...

but as far as I can tell this function is never defined and I cannot find it as a module function of node.js, a function of jquery or a standard function for javascript. 但据我所知,这个函数从未定义过,也找不到它作为node.js的模块函数,jquery的函数或javascript的标准函数。

I think I understand how callback functions work but I'm not familiar with this call and it is used frequently enough in this app that I would like a firm understanding of what it is and where it comes from. 我想我了解回调函数的工作原理,但是我对该调用不熟悉,并且在该应用程序中经常使用它,因此我希望对它的含义及其来源有一个深刻的了解。

So my question is three fold: 1) where is the function based: (javascirpt, jquery, node.js, particular to this app) 2) where can I find the source code for this function? 因此,我的问题有三点:1)函数基于哪里:(javascirpt,jquery,node.js,特定于此应用)2)在哪里可以找到此函数的源代码? 3) how is this function interacting with the functions it is called in? 3)这个函数如何与它所调用的函数进行交互?

It is the argument of the loadResponseData function. 它是loadResponseData函数的参数。 If you call loadResponseData like this: 如果像这样调用loadResponseData

loadResponseData(function () {
      res.writeHead(200, headers);
      res.end(req.method === "HEAD" ? "" : body);
    });

then callback() in loadResponseData will execute 然后将执行loadResponseData callback()

res.writeHead(200, headers);
res.end(req.method === "HEAD" ? "" : body);

EDIT to clarify the question in comments: 编辑以澄清评论中的问题:

You could say it's a feature of JavaScript. 您可以说这是JavaScript的功能。 The important thing here is that JavaScript is a functional language: it has functions as a data type in their own right. 这里重要的是,JavaScript是一种功能语言:它本身具有作为数据类型的功能。 Thus, you can save them in variables (and indeed, that's all every function name in JS is - a variable with a function as its content), and pass them along in an argument list (as is demonstrated here). 因此,您可以将它们保存在变量中(实际上,JS中的所有函数名称都是-以函数为内容的变量),然后将它们传递到参数列表中(如此处所示)。 There is nothing magical about the name callback - it could have as well been fn or whoopsie7 . 名称callback没有什么神奇的-它可能是fnwhoopsie7 To demonstrate: 展示:

var doubleAndOne = function(a) {
  return a * 2 + 1;
}
function doItTwice(k, whoopsie7) {
  whoopsie7(whoopsie7(k));
};
doItTwice(5, doubleAndOne); // result is 23

function(...) {...} is called an anonymous function: it is pure function value, taking some arguments and doing something with them, but it is not assigned to any name. function(...) {...}被称为匿名函数:它是纯函数值,带有一些参数并对其进行处理,但未分配任何名称。 To make a function with a name, you can assign this to a variable, or pass it as a parameter, just like you could with any other value. 要使用名称创建函数,可以将其分配给变量,也可以将其作为参数传递,就像使用其他任何值一样。 For example, there is very little difference between: 例如,它们之间的区别很小:

var five = function() { return 5; };
var doubleFuncValue = function(fn) { return fn() * 2; };
doubleFuncValue(five); // result 10

and

var five = 5;
var doubleNumValue = function(n) { return n * 2; };
doubleNumValue(five); // result 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM