使用 Pokémon Evolution 解释函数组合

2025-06-04

使用 Pokémon Evolution 解释函数组合

哟,(围巾)狗。我听说你喜欢函数,所以我在你的函数里加了一个函数,这样你就可以一边运行一边运行。</ oldMemes >< / badJoke >

伊布摇着头,不赞同我的冷笑话

无论如何,我的朋友开始学习如何编码,她需要帮助来理解老师要求她做什么。

这是她发给我的:

/*
Create a function that takes in two inputs.
One should be a function and the other should be
the argument to call the input function with.

Then in the function you define call the passed in function
with the input argument. 
*/
Enter fullscreen mode Exit fullscreen mode

...不好意思,但是哈哈哈?

以下是她发送的示例函数:

function sayHi(b,c){
c=prompt("Greet me!");
b(c);
Enter fullscreen mode Exit fullscreen mode

好的,我想这样就更清楚一点了。


那么,我们开始吧:

据我了解,我们希望构建一个函数来运行另一个函数。我使用了宝可梦进化石头的概念来说明这一点。

伊布在《大侦探皮卡丘》电影中的进化

进化是宇宙的整体功能。

function evolutionFn(pokemon, stone){
  stone = prompt('Which stone will you use?');
  return pokemon(stone);
}
Enter fullscreen mode Exit fullscreen mode

宝可梦本身拥有不同的功能,但普遍使用相同的进化功能。伊布就是最好的例子,因为它们进化的可能性非常巨大。(不过目前,我们参考的是它们第一代的基础进化。)

伊布将花冠抛到头上

const eevee = (x) => {
  let userInput = x.toLowerCase();
  if ( userInput === 'fire' ){
    return 'Congrats! You now have a Flareon!'
  }else if( userInput ==='thunder' ){
    return 'Congrats! You now have a Jolteon!'
  } else if( userInput === 'water' ){
    return 'Congrats! You now have a Vaporeon!'
  } else {
    return 'Huh. It didn\'t work.'
  }
}
Enter fullscreen mode Exit fullscreen mode

我还创造了皮卡丘来说明完全不同的神奇宝贝也可以利用这种进化方法。

雷丘旋转皮卡丘跳舞

const pikachu = (x) => {
  let userInput = x.toLowerCase();
  if ( userInput === 'thunder'){
    return 'Congrats! You now have a Raichu!'
  } else {
    return 'Huh. It didn\'t work.'
  }
}
Enter fullscreen mode Exit fullscreen mode

综合起来,我们得到以下结果:

function evolutionFn(pokemon, stone){
  stone = prompt('Which stone will you use?');
  return pokemon(stone);
}

const eevee = (x) => {
  let userInput = x.toLowerCase();
  if ( userInput === 'fire' ){
    return 'Congrats! You now have a Flareon!'
  }else if( userInput ==='thunder' ){
    return 'Congrats! You now have a Jolteon!'
  } else if( userInput === 'water' ){
    return 'Congrats! You now have a Vaporeon!'
  } else {
    return 'Huh. It didn\'t work.'
  }
}

const pikachu = (x) => {
  let userInput = x.toLowerCase();
  if ( userInput === 'thunder'){
    return 'Congrats! You now have a Raichu!'
  } else {
    return 'Huh. It didn\'t work.'
  }
}

console.log(evolutionFn(eevee));
// example: if prompt => 'fire or FIRE or even FiRe', 
// it will say "Congrats! You now have a Flareon!"
// if it's anything else, the console will return "Huh. It didn't work."


console.log(evolutionFn(pikachu));
// Should return "Congrats you now have a Raichu"! etc. etc.
Enter fullscreen mode Exit fullscreen mode

在Repl.it上实时玩它


这就是函数组合:当你想使用一个大函数来执行一些小函数时,这些小函数基本上会输出相同的基本内容。

另外,有趣的是——你被蒙蔽了!如果你用过 .map()、.split()、.join()、.reverse(),那你肯定体验过函数组合!我们经常使用 JavaScript 方法时,就能体会到这一点。


感谢阅读!

如果你想与我保持联系,请在Twitter上关注我!DM 已开放。

此外,请订阅我的时事通讯,我会在其中分享一些个人(有时令人尴尬的)故事,为您提供有关如何在编码训练营和训练营后/自学中生存的技巧/窍门!

文章来源:https://dev.to/cat/function-composition-explained-using-pokemon-evolution-1jhn
PREV
面试问答解码 - # 1
NEXT
弗兰肯代码,直到你成功。