2017-07-17 79 views
6
const fetch = url => dispatch => { 
    // ... 
} 

export const fetchQuestions = tag => (dispatch) => { 
    return dispatch(fetch(tag)); 
}; 

什么是dispatchfetch函数中? url是第一个和单个参数fetch函数。但是dispatch在这里?链式箭头功能语法

+2

这就是所谓的钻营。去谷歌上查询。 –

+1

你可以看到它的行动。有一些es6 => es5转换器在线。试试这个例子:https://es6console.com/。将代码粘贴到编辑器中,然后单击“变换” – Victor

+0

将根据您的代表降低此值,但我意识到您没有参与JavaScript。 – BotNet

回答

5

这相当于一个函数返回另一个函数。即此

const fetch = url => dispatch => { 
    // ... 
} 

相当于

const fetch = function(url) { 
    return function(dispatch) { 
     // ... 
    } 
} 

同样这

export const fetchQuestions = tag => (dispatch) => { 
    return dispatch(fetch(tag)); 
}; 

相当于

export const fetchQuestions = function(tag) { 
    return function(dispatch) { 
     return dispatch(fetch(tag)); 
    } 
}; 
1

dispatchurl => ...函数返回的函数的第一个参数和单个参数。使用正常的函数语法,它将是

const fetch = function(url) { 
    return function(dispatch) {...} 
} 
1

它的写作功能的更短的方式returns another function。这些参数urldispatch有观点的curryed function的ES5相当于箭头函数的语法将是

function fetch(url) { 
    return function(dispatch) { 
     .... 
    } 
} 

或箭头语法

const fetch = (url) => { 
    return (dispatch) => { 
     // ... 
    } 
} 

同样你会fetchQuestion写成

export function fetchQuestions(tag) { 
    return function(dispatch){ 
      return dispatch(fetch(tag)); 
    } 
} 

或使用箭头语法为

export const fetchQuestions = (tag) => { 
    return (dispatch) => { 
     return dispatch(fetch(tag)); 
    } 
}; 
0

fetch名为函数表达,需要一个url参数并返回一个新的函数,它接受一个dispatch参数。

你可以重写使用传统函数的语法:

const fetch = function (url) { 
    return function(dispatch) { 
    // ... 
    } 
}