Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么';这段代码中的箭头函数是什么?_Javascript_Callback_Arrow Functions_Ipfs - Fatal编程技术网

Javascript 什么';这段代码中的箭头函数是什么?

Javascript 什么';这段代码中的箭头函数是什么?,javascript,callback,arrow-functions,ipfs,Javascript,Callback,Arrow Functions,Ipfs,代码来自IPFS(Inter planetary file system)HTTP API JS实现: 所描述的函数是add()函数,用于将数据推送到IPFS 首先,我将解释我所理解的:add()函数接受三个参数–如果没有options对象(用户省略了它),并且它被一个函数替换:用户尝试实现回调函数–将回调改为optscb=选项 其次,如果引用的文件是一个文本文件&&以http开头-它显然是远程托管的,我们需要使用crask获取它 所有这些我都理解,但是为什么我们要使用(send)=>箭头功能呢

代码来自IPFS(Inter planetary file system)HTTP API JS实现:

所描述的函数是
add()
函数,用于将数据推送到IPFS

首先,我将解释我所理解的:
add()
函数接受三个参数–如果没有
options
对象(用户省略了它),并且它被一个函数替换:用户尝试实现回调函数–将回调改为
opts
<代码>cb=选项

其次,如果引用的文件是一个文本文件
&&
http
开头-它显然是远程托管的,我们需要使用
crask
获取它


所有这些我都理解,但是为什么我们要使用
(send)=>
箭头功能呢?为什么要使用返回函数add…?发送('add',null,opts,res,cb)和返回发送('add',null,opts,res,cb)用于什么?回调(
cb
)是如何实现的?帮助我理解这里发生了什么

导出的整个东西是一个函数,它期望
send
作为一个参数;这允许调用代码通过传入要使用的
send
函数来执行依赖项注入。它的预期用途如下:

let addBuilder = require("add");
let add = addBuilder(senderFunction);
// This function ----^
// is `send` in the `add.js` file.
// It does the actual work of sending the command

// Then being used:
add(someFiles, someOptions, () => {
    // This is the add callback, which is `cb` in the `add.js` file
});
(通常,上面的前两条语句被写成一条语句,例如,
let add=require(“add”)(senderFunction);


基本上,整个过程就是一个大型的构建器,它使用一个给定的
send
函数,通过调用构建器将该函数注入其中。这样,就可以通过注入测试版本的
send
进行测试,并通过注入真正版本的
send
进行实际使用;和/或各种“真实”版本的
send
可用于不同的环境、传输机制等。

这只是一个工厂函数,生成其他函数并在内部调用它们。模块不再需要它,但有些人仍然坚持这种模式。@ssube:它被用来进行依赖项注入(
send
作为被注入的依赖项)。模块如何使其变得不必要?(真正的问题。)
let addBuilder = require("add");
let add = addBuilder(senderFunction);
// This function ----^
// is `send` in the `add.js` file.
// It does the actual work of sending the command

// Then being used:
add(someFiles, someOptions, () => {
    // This is the add callback, which is `cb` in the `add.js` file
});