js 多个箭头函数的使用

多个箭头函数,例如:

const navigateOnce = (getStateForAction) => (action, state) => {
    const {type, routeName, params} = action;
    return (
        state &&
        (type === NavigationActions.NAVIGATE) &&
        routeName === state.routes[state.routes.length - 1].routeName &&
        JSON.stringify(params) === JSON.stringify(state.routes[state.routes.length - 1].params)
    ) ? null getStateForAction(action, state);
};

箭头函数的含义:

() => ...
//等价于
(function() {
    return ...
}).bind(this)

注意: 箭头函数在不写{} 的情况下,可以省略return关键字,而默认return接下来的东西

由此可见:类似

() => () => ...

等价于:

() => {return () => {return ...}}

等价于

function () {
    retunrn function() {
        return ...
    }
}

综上,上面函数的意思就是:

const navigateOnce = function(getStateForAction) {
    return (action, state) => {
        const {type, routeName, params} = action;
        return (
            state &&
            (type === NavigationActions.NAVIGATE) &&
            routeName === state.routes[state.routes.length - 1].routeName &&
            JSON.stringify(params) === JSON.stringify(state.routes[state.routes.length - 1].params)
        ) ? null getStateForAction(action, state);
    };
}

即:

const navigateOnce = function(getStateForAction) {
    return function(action, state) {
        const {type, routeName, params} = action;
        return (
            state &&
            (type === NavigationActions.NAVIGATE) &&
            routeName === state.routes[state.routes.length - 1].routeName &&
            JSON.stringify(params) === JSON.stringify(state.routes[state.routes.length - 1].params)
        ) ? null getStateForAction(action, state);
    };
}
原文地址:https://www.cnblogs.com/nangezi/p/12346471.html