本文介绍了使用箭头函数将参数传递给回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know this is a duplicated question with ES5, but I am looking for the syntax with ES6 arrow function. My code below:

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

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

对于上述情况,我想将一些参数( myParams )与函数调用一起传递,我该如何实现?

For the above scenario, I want to pass some parameters(myParams) along with the function call, how can I achieve that?

推荐答案

您可以这样做:

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

用法示例:

const fetchItems = (callback, ...params) => {
    callback(params);
}

fetchItems (console.log, 'foo', 1);

这篇关于使用箭头函数将参数传递给回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:55