我有一个函数callWithMagic,将回调函数作为参数,并用一个参数调用它。
callWithMagic
const callWithMagic = callback => { const magic = getMagic(); callback(magic); };
我还有一个带有processMagic两个参数的函数:magic和theAnswer。
processMagic
magic
theAnswer
const processMagic = (magic, theAnswer) => { someOtherMagic(); };
我想将函数processMagic作为参数传递给callWithMagic,但我也想42作为第二个参数(theAnswer)传递给processMagic。我怎样才能做到这一点?
42
callWithMagic(<what should I put here?>);
只需创建一个包装回调:
callWithMagic(function(magic) { return processMagic(magic, 42); });
或使用ECMAScript 6箭头功能:
callWithMagic(magic => processMagic(magic, 42));