使用箭头函数将参数传递给回调函数

Jai*_*son 6 javascript ecmascript-6 arrow-functions

我知道这是ES5的一个重复问题,但是我正在寻找ES6箭头功能的语法。我的代码如下。

fetchItems = (callback) => {
    //After ajax success
    callback(response);
}

const myParams = {name:"John"}
this.fetchItems((res) => {
    console.log(res.data);
});
Run Code Online (Sandbox Code Playgroud)

对于上述情况,我想将一些参数(myParams)与函数调用一起传递并进入函数,如何归档该文件,请帮忙。

Twi*_*her 6

您可以这样做:

const fetchItems = (callback, ...params) => {
    //Do whatever you want with the params
    callback(response);
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

const fetchItems = (callback, ...params) => {
    //Do whatever you want with the params
    callback(response);
}
Run Code Online (Sandbox Code Playgroud)