繁体   English   中英

链式箭头函数语法

[英]Chained Arrow function syntax

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

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

什么是fetch函数中的dispatch url是第一个和单个参数fetch功能。 但是什么在这里dispatch

这相当于一个函数返回另一个函数。 就是这个

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));
    }
};

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

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

它是编写returns another function的函数的较短方式。 参数urldispatchcurryed函数的参数。箭头函数语法的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));
    }
};

fetch是一个命名函数表达式,它接受url参数并返回一个带有dispatch参数的新函数。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM