什么是链式ES6箭头功能?

时间:2018-05-14 11:24:55

标签: javascript function ecmascript-6 redux arrow-functions

我正在使用React,Redux创建一个应用程序。

其中,我正在制作Redux中间件,

有一部分我不明白。

以下是代码:

const loggerMiddleware = store => next => action => {

    console.log('currentState', store.getState());

    console.log('action', action);

    const result = next(action);

    console.log(', store.getState());
    console.log('\n'); 

    return result;
}

export default loggerMiddleware; 

这个箭头函数=> => =>是什么? 箭头功能继续没有意义。

这是什么意思?

3 个答案:

答案 0 :(得分:3)

以下代码:

const loggerMiddleware = store => next => action => { 
    var result = /* .. */
    return result;
}

相当于:

const loggerMiddleware =  function(store) { 
    return function(next) {
        return function(action) {
            return result;
        }
    }
}

答案 1 :(得分:3)

这是一种技术(称为currying),它取代了一个函数,该函数接受一些带有多个函数的参数,每个函数都占用这些参数的一部分,例如:

    const f1 = (x, b) => x + b;
    const f2 = x => b => x + b;
    
    const f1Result = f1(1, 2);
    // we can construct f2's result in multiple steps if we want because it returns a function, this can be helpful.
    let f2Result = f2(1);
    f2Result = f2Result(2);

    console.log('f1Result', f1Result);
    console.log('f2Result', f2Result);

您还可以阅读this,了解有关此决定背后理由的更多信息,主要是:

  

有时我们希望将某个本地状态与商店和下一个

相关联

答案 2 :(得分:0)

Javascript箭头功能是8个字符'function'关键字的替代品。当您使用箭头功能时,您不需要在函数内写回。

根据Javascript最佳做法,在您拨打服务电话时尝试使用。它用作函数关键字简写。

有关详情,请参阅此链接上的一个好示例https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions