将附加参数传递给 Javascript 回调函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11371673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass additional parameter to Javascript callback function
提问by Dan Hlavenka
I need to watch a small number of directories in a Node.JS application:
我需要在 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);
The problem is that fs.watch only passes the filename to the callback function, without including the directory it's in. Is there a way I can somehow pass in an extra parameter to the updated() function so it knows where the file is?
问题是 fs.watch 只将文件名传递给回调函数,而不包括它所在的目录。有没有办法以某种方式将额外的参数传递给 updated() 函数,以便它知道文件在哪里?
I think I'm looking for something similar to Python's functools.partial
, if that helps any.
我想我正在寻找类似于 Python 的东西functools.partial
,如果有帮助的话。
采纳答案by Ry-
You can use Function.bind
:
您可以使用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/'));
回答by Scott Sauyet
You can pass a different function for each iteration:
您可以为每次迭代传递不同的函数:
var getUpdatedFunction = function(folderName) {
return function(event, filename) {
log("CHANGED\t" + folderName + "/" + filename);
};
};
for(i in channels) {
foldername = 'share/channels/' + channels[i];
fs.watch(foldername, {persistent: false}, getUpdatedFunction(foldername));
}
回答by ThorSummoner
Example using JS Bind
使用 JS 绑定的示例
Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
文档:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
Tip, the bound parameters occur before the call-time parameters.
提示,绑定参数出现在调用时参数之前。
my_hello = 'Hello!'
my_world = {
'antartica': 'cold',
}
anonymous_callback = function (injected1, injected2, param1, param2) {
param1 = param1 ? param1 : 'One';
param2 = param2 ? param2 : 'Two';
console.log('param1: (' + typeof(param1) + ') ' + param1)
console.log('param2: (' + typeof(param2) + ') ' + param2)
console.log('injected1: (' + typeof(injected1) + ') ' + injected1)
console.log('injected2: (' + typeof(injected2) + ') ' + injected2)
console.log(injected2)
}.bind(this, my_hello, my_world)
anonymous_callback('Param 1', 'Param 2')
Output:
输出:
param1: (string) Param 1
param2: (string) Param 2
injected1: (string) Hello!
injected2: (object) [object Object]
{ antartica: 'cold' }
回答by rusffer
You can pass additional callback in place
您可以就地传递额外的回调
function updated(channel, filename) {
log('CHANGED\t ' + channel + '/' + filename);
}
for(i in channels) {
channel = '/share/channels/' + channels[i];
fs.watch(channel, {persistent: false}, function (extra, event, filename) {
updated(channel, filename);
});
}