小编典典

将附加参数传递给Javascript回调函数

node.js

我需要在Node.JS应用程序中观察少量目录:

function updated(event, filename){
    log("CHANGED\t/share/channels/" + filename);
}
for(i in channels)
    fs.watch('share/channels/' + channels[i], {persistent: false}, updated);

问题在于fs.watch仅将文件名传递给回调函数,而没有包括它所在的目录。我是否可以通过某种方式将额外的参数传递给updated()函数,以便它知道文件在哪里?

我认为我正在寻找类似于Python的工具functools.partial,如果有帮助的话。


阅读 383

收藏
2020-07-07

共1个答案

小编典典

您可以使用Function.bind

function updated(extraInformation, event, filename) {
    log("CHANGED\t/share/channels/" + extraInformation + filename);
}

for(i in channels)
    fs.watch('share/channels/' + channels[i], {persistent: false},
              updated.bind(null, 'wherever/it/is/'));
2020-07-07