Javascript回调两个函数 [英] Javascript callback for two functions
本文介绍了Javascript回调两个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法用Javascript(ES6)实现代码?
Is there a way to achieve the code bellow with Javascript (ES6)?
如果是,我该怎么办?我试试这个例子,但它没有用。
If yes, how can I do it? I try this example, but it didn't work.
const funcA = (callback, arg1) => {
console.log("Print arg1: " + arg1); /* Print arg1: argument1 */
let x = 0;
x = callback(x, );
return x;
}
const funcB = (x, prefix) => {
console.log("Print prefix: " + prefix); /* Print prefix: PREFIX_ */
x = x + 1;
return x;
}
/* Exec function funcA */
let x = funcA(funcB( ,"PREFIX_"), "argument1");
console.log("Value of x: " + x); /* Value of x: 1 */
推荐答案
部分申请在js中还不可能。你需要另一个箭头函数作为回调函数:
Partial application is not yet possible in js. You need another arrow function that acts as a callback:
funcA(x => funcB(x ,"PREFIX_"), "argument1");
要打电话说你不需要额外的逗号:
To call that you don't need that extra comma:
x = callback(x)
当此提案可能允许写下此内容时:
Somewhen this proposal might allow to write this:
funcA( funcB(?, "PREFIX_"), "argument1")
这篇关于Javascript回调两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文