【问题标题】:callback functions, error when passing multiple callback functions回调函数,传递多个回调函数时出错
【发布时间】:2021-05-07 04:44:52
【问题描述】:

为此,我构建了几个函数和一个高阶函数。我希望将三个较小的函数作为参数传递给高阶函数。当我尝试调用高阶函数并作为参数传递时,出现回调不是函数的错误。有什么想法吗?

  {
    "Year": 2002,
    "Datetime": "02 Jun 2002 - 18:30",
    "Stage": "Group F",
    "Stadium": "Saitama Stadium 2002",
    "City": "Saitama",
    "Home Team Name": "England",
    "Home Team Goals": 1,
    "Away Team Goals": 1,
    "Away Team Name": "Sweden",
    "Win conditions": "",
    "Attendance": 52721,
    "Half-time Home Goals": 1,
    "Half-time Away Goals": 0,
    "Referee": "SIMON Carlos (BRA)",
    "Assistant 1": "OLIVEIRA Jorge (BRA)",
    "Assistant 2": "DUPANOV Yuri (BLR)",
    "RoundID": 43950100,
    "MatchID": 43950005,
    "Home Team Initials": "ENG",
    "Away Team Initials": "SWE"
  },
  {
    "Year": 2002,
    "Datetime": "02 Jun 2002 - 20:30",
    "Stage": "Group B",
    "Stadium": "Gwangju World Cup Stadium",
    "City": "Gwangju",
    "Home Team Name": "Spain",
    "Home Team Goals": 3,
    "Away Team Goals": 1,
    "Away Team Name": "Slovenia",
    "Win conditions": "",
    "Attendance": 28598,
    "Half-time Home Goals": 1,
    "Half-time Away Goals": 0,
    "Referee": "GUEZZAZ Mohammed (MAR)",
    "Assistant 1": "TOMUSANGE Ali (UGA)",
    "Assistant 2": "BEREUTER Egon (AUT)",
    "RoundID": 43950100,
    "MatchID": 43950008,
    "Home Team Initials": "ESP",
    "Away Team Initials": "SVN"
  },
]

function getFinals(arr) 
{
    return arr.filter(team => team.Stage === "Final");
}
//console.log(getFinals(fifaData));

function getYears(arr, callback) 
{
   const years = callback(arr).map(team => team.Year);
   return years;
}

function getWinners(arr, func) 
{
    const winners = [];
    const winIt = func(arr);
    console.log(winIt);
    for (let i = 0; i < winIt.length; i++)
    {
      if(winIt[i]['Home Team Goals'] > winIt[i]['Away Team Goals'])
      {
        winners.push(winIt[i]['Home Team Name']);
      }

      else
      {
        winners.push(winIt[i]['Away Team Name']);
      }
    }

  return winners
}


function getWinnersByYear(arr, cbThree, cbFour,cbFive) 
{
    const years = cbThree(arr, cbFive);
    const winners = cbFour(arr, cbThree);
    const full = [];
    for(let j = 0; j < years.length; j++)
    {
      full.push(`In ${years[j]}, ${winners[j]} won the world cup!`);
    }
    return full;
}
console.log(getWinnersByYear(fifaData, getYears, getWinners, getFinals));

【问题讨论】:

    标签: javascript callback higher-order-functions


    【解决方案1】:

    您正在调用getYears,而getWinners 中只有一个参数:

    function getWinners(arr, func) // <- getYears was passed as `func`
    {
        const winners = [];
        const winIt = func(arr); // <- here
    

    在 JS 中,在函数调用中省略参数相当于为该参数提供undefined

    所以在这个调用中,参数callback被赋值为undefined,这确实不是一个函数(因此不能被调用)。


    我建议为您的参数指定有意义的名称。它提高了可读性并有助于追踪此类错误。

    一般流程也可以简化,减少对回调的大量使用——例如,在getYears:考虑如何直接传递callback(arr)而不是arrcallback的结果会更简单,并且等价的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多