Pase un argumento adicional a una función de devolución de llamada
Tengo una función callWithMagic
que toma una función de devolución de llamada como parámetro y la llama con un argumento.
const callWithMagic = callback => {
const magic = getMagic();
callback(magic);
};
También tengo una función processMagic
que toma dos argumentos: magic
y theAnswer
.
const processMagic = (magic, theAnswer) => {
someOtherMagic();
};
Quiero pasar la función processMagic
como argumento a callWithMagic
, pero también quiero pasar 42
como segundo parámetro (theAnswer
) a processMagic
. ¿Cómo puedo hacer eso?
callWithMagic(<what should I put here?>);
28
Author: Michał Perłakowski, 2016-11-25
4 answers
Simplemente crea un callback wrapper:
callWithMagic(function(magic) {
return processMagic(magic, 42);
});
O usando ECMAScript 6 funciones de flecha :
callWithMagic(magic => processMagic(magic, 42));
32
Author: str,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 16:31:05
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 16:31:05
Podría usar una función anonymus
Algo como
session.sub('Hello', function(){marketEvents(your args);});
1
Author: Dropye,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 09:56:17
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 09:56:17
Puede crear una función que llame a la función marketEvent
. No hay necesidad de complicar las cosas
session.sub('Hello', function(args, kwargs) {
marketEvent(args, kwargs, 'my custom data');
});
De lo contrario puedes hacer esto:
var mrktEvent = function(customArgs) {
return function(args, kwargs) {
marketEvent(args, kwargs, customArgs)
};
}
session.sub('Hello', mrktEvent("customEvent"));
1
Author: TryingToImprove,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 09:59:12
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 09:59:12
Puede enlazar el objeto argumento a la función callback:
var varObject = {var1: "findButton", var2: true};
function cbFunc() {
console.log(this.var1+ ":" + this.var2);
}
//Example callback
datatable.ajax.reload(cbFunc.bind(varObject));
0
Author: aecavac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-25 08:25:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-25 08:25:15