将额外的参数传递给回调函数
Posted
技术标签:
【中文标题】将额外的参数传递给回调函数【英文标题】:Pass an extra argument to a callback function 【发布时间】:2017-04-09 16:22:50 【问题描述】:我有一个函数callWithMagic
,它接受一个回调函数作为参数并用一个参数调用它。
const callWithMagic = callback =>
const magic = getMagic();
callback(magic);
;
我还有一个函数 processMagic
,它接受两个参数:magic
和 theAnswer
。
const processMagic = (magic, theAnswer) =>
someOtherMagic();
;
我想将函数processMagic
作为参数传递给callWithMagic
,但我也想将42
作为第二个参数(theAnswer
)传递给processMagic
。我该怎么做?
callWithMagic(<what should I put here?>);
【问题讨论】:
如果你想看到一个更彻底的答案,请看这个问题和答案,它是两年前的:***.com/questions/21985201/… 【参考方案1】:只需创建一个function(magic)
作为包装回调:
callWithMagic(function(magic)
return processMagic(magic, 42);
);
或者使用ECMAScript 6: arrow functions:
callWithMagic(magic => processMagic(magic, 42));
【讨论】:
谢谢,这就是“工作”!但只是第一次调用 marketEvent 。下一个调用返回 undefined 对于 newArg 值,但不是其他两个。 @Kulin 这应该每次都有效。当您将此标记为答案时,您是否让它工作? 这篇文章正在 Meta - meta.***.com/q/338481 上进行讨论(请参阅问题的 cmets)。同时通知提问者@Kulin。【参考方案2】:你可以使用匿名函数
类似
session.sub('Hello', function()marketEvents(your args););
【讨论】:
这是迄今为止最好的答案,谢谢!也可以与 function() 上的参数一起使用,例如session.sub('Hello', function(orgArg)marketEvents(orgArg, your_args););【参考方案3】:您可以创建一个调用marketEvent
函数的函数。不需要把事情复杂化
session.sub('Hello', function(args, kwargs)
marketEvent(args, kwargs, 'my custom data');
);
否则你可以这样做:
var mrktEvent = function(customArgs)
return function(args, kwargs)
marketEvent(args, kwargs, customArgs)
;
session.sub('Hello', mrktEvent("customEvent"));
【讨论】:
【参考方案4】:您可以将参数对象绑定到回调函数:
var varObject = var1: "findButton", var2: true;
function cbFunc()
console.log(this.var1+ ":" + this.var2);
//Example callback
datatable.ajax.reload(cbFunc.bind(varObject));
【讨论】:
【参考方案5】:我想将函数
processMagic
作为参数传递给callWithMagic
,但我也想将42
作为第二个参数theAnswer
传递给processMagic
。我该怎么做?
好的,首先,您的实现几乎接近答案。你想这样调用你的函数callWithMagic(<what should I put here?>);
。
因此,callWithMagic(<what should I put here?>);
应替换为 callWithMagic(processMagic, 42);
,其中第一个参数是 callback
函数,另一个是您要添加的额外参数。
为了能够使用额外参数调用此函数,我们需要修改callWithMagic
的实现,通过定义一个新参数answer
,如下所示:
const callWithMagic = (callback, answer) =>
const magic = getMagic();
callback(magic, answer);
;
现在,您的实现有了一个有效的 sn-p:
const spells = ["Accio", "Aguamenti", "Alohomora", "Aparecium", "Avada Kedavra", "Avifors", "Avis", "Bombarda", "Colloportus", "Confringo", "Confundus", "Crucio", "Deletrius", "Densaugeo", "Diffindo", "Dissendium", "Engorgio", "Episkey", "Evanesco", "Expecto Patronum", "Expelliarmus", "Fera Verto", "Ferula", "Fidelius", "Finite Incantatem", "Flagrate", "Flipendo", "Furnunculus", "Geminio", "Homorphus", "Immobulus", "Impedimenta", "Imperio", "Impervius", "Incarcerous", "Incendio", "Legilimens", "Levicorpus", "Liberacorpus", "Locomotor Mortis", "Lumos", "Mobiliarbus", "Mobilicorpus", "Morsmordre", "Muffliato", "Nox", "Obliviate", "Orchideous", "Petrificus Totalus", "Prior Incantato", "Protego", "Reducio", "Reducto", "Relashio", "Rennervate", "Reparo", "Repello", "Repello Muggletum", "Revelio", "Rictusempra", "Riddikulus", "Salvio Hexia", "Scourgify", "Sectumsempra", "Serpensortia", "Silencio", "Sonorus", "Stupefy", "Tarantallegra", "Tergeo", "Waddiwasi", "Wingardium Leviosa"];
const len = spells.length;
function random(max)
return Math.floor(Math.random() * max);
const getMagic = () =>
return spells[random(len)];
const callWithMagic = (callback, answer) =>
const magic = getMagic();
callback(magic, answer);
;
const someOtherMagic = (magic, theAnswer) =>
console.log(
magic,
theAnswer
)
;
const processMagic = (magic, theAnswer) =>
someOtherMagic(magic, theAnswer);
;
callWithMagic(processMagic, 42);
【讨论】:
以上是关于将额外的参数传递给回调函数的主要内容,如果未能解决你的问题,请参考以下文章