Javascript 不将新函数的参数理解为参数
我遵循一个教程,使图像上传系统 现在使用以下功能进行上传,并使用外部模块强大Javascript 不将新函数的参数理解为参数,javascript,Javascript,我遵循一个教程,使图像上传系统 现在使用以下功能进行上传,并使用外部模块强大 function upload(response, request) { console.log("Request handler 'upload' was called."); var form = new formidable.IncomingForm(); console.log("about to parse"); form.parse(request, function(error, fie
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files) {
console.log("parsing done");
fs.rename(files.upload.path, "/tmp/test.png",
function(error) {
if (error) {
fs.unlink("/tmp/test.png");
fs.rename(files.upload.path, "/tmp/test.png");
}
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
});
}
功能上传(响应、请求){
log(“调用了请求处理程序'upload'”);
var form=new.IncomingForm();
log(“即将解析”);
解析(请求、函数(错误、字段、文件){
log(“解析完成”);
重命名(files.upload.path,“/tmp/test.png”,
函数(错误){
如果(错误){
fs.unlink(“/tmp/test.png”);
重命名(files.upload.path,“/tmp/test.png”);
}
});
writeHead(200,{“内容类型”:“text/html”});
响应。写入(“接收到的图像:
”);
回答。写(“”);
response.end();
});
}
form.parse接受2个参数,请求和一个新函数,但新函数接受3个新参数
我似乎不明白。这3个新论点来自哪里?这与外部库有关,还是我没有正确理解JavaScript
提前感谢。第二个参数传递给form.parse方法,因为第二个参数是回调函数。解析表单后,form.parse将调用它。回调函数的参数值来自form.parse方法。
form.parse
取2个参数,第二个参数是名为回调的函数。回调从form.parse
实现中获取参数
让我们解释一个更简单的例子:
function foo(callback) {
var arg1 = 10;
var arg2 = 'string';
var arg3 = false;
// call function with args
callback(arg1, arg2, arg3);
};
function callback(num, str, bool) {
// we get args from foo implementation
console.log(num);
console.log(str);
console.log(bool);
}
foo(callback);
在foo
code中,我们使用3个参数调用callback
。现在我们可以在回调
函数中使用它们。考虑这一点的方式是form.parse
接受两个参数—第一个由请求指示,第二个是函数。第二个函数作为参数传递给form.parse
,以便该函数可以在某个时刻调用它
您可能会更清楚地看到它是这样写的(这是等效的,但可以说更清楚):
功能上传(响应、请求){
log(“调用了请求处理程序'upload'”);
var process_request=函数(错误、字段、文件){
log(“解析完成”);
重命名(files.upload.path,“/tmp/test.png”,
函数(错误){
如果(错误){
fs.unlink(“/tmp/test.png”);
重命名(files.upload.path,“/tmp/test.png”);
}
});
writeHead(200,{“内容类型”:“text/html”});
响应。写入(“接收到的图像:
”);
回答。写(“”);
response.end();
};
var form=new.IncomingForm();
log(“即将解析”);
解析(请求、处理请求);
}
在本例中,我们声明了一个函数process\u request
,并将其传递给form.parse
。该函数将在某个时间点“回调”,并且在调用时将使用3个参数调用它(最有可能,但不保证)。这是外部库所期望的回调函数。你真的应该去读JS的基础知识。然后找到实现parse
函数的源代码。
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var process_request = function(error, fields, files) {
console.log("parsing done");
fs.rename(files.upload.path, "/tmp/test.png",
function(error) {
if (error) {
fs.unlink("/tmp/test.png");
fs.rename(files.upload.path, "/tmp/test.png");
}
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
};
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, process_request );
}