多個箭頭函數,例如:
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); }; }
