将附加参数传递给Javascript回调函数[重复]

时间:2023-02-12 09:56:36

This question already has an answer here:

这个问题在这里已有答案:

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的东西,如果有帮助的话。

4 个解决方案

#1


4  

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/'));

#2


11  

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));
}

#3


5  

Example using JS Bind

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

Doc: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' }

#4


0  

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);
  });
}

#1


4  

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/'));

#2


11  

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));
}

#3


5  

Example using JS Bind

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

Doc: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' }

#4


0  

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);
  });
}