2017-07-17 40 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) { 
    // ... 
    } 
}