【问题标题】:Eloquent JavaScript Book : don't understand the exampleEloquent JavaScript Book : 不懂例子
【发布时间】:2020-12-18 16:14:50
【问题描述】:

在第 3 章中,有这个 Hummus 食谱示例,我真的不明白的是这样的代码:

const hummus = function(factor) {
  const ingredient = function(amount, unit, name) {
    let ingredientAmount = amount * factor;
    if (ingredientAmount > 1) {
      unit += "s";
    }
    console.log(`${ingredientAmount} ${unit} ${name}`);
  };
  ingredient(1, "can", "chickpeas");
  ingredient(0.25, "cup", "tahini");
  ingredient(0.25, "cup", "lemon juice");
  ingredient(1, "clove", "garlic");
  ingredient(2, "tablespoon", "olive oil");
  ingredient(0.5, "teaspoon", "cumin");
};

我添加了一些 console.log 以尝试更好地跟踪进度并了解此处实际发生的情况,但我无法理解它。我的意思是这里的预期结果是什么?

【问题讨论】:

    标签: function eloquent console.log progress recipe


    【解决方案1】:

    该示例的目的是向您展示函数范围。通过将固定参数传递给鹰嘴豆泥函数,您可以返回一些新的文字对象,该对象将通过使用您的内部函数和外部函数参数因子来计算最终结果。

    【讨论】:

      【解决方案2】:

      只需添加鹰嘴豆泥(数量)即可获得成分输出

      鹰嘴豆泥(5);在代码末尾调用函数

      输出: 5罐鹰嘴豆 1.25 杯芝麻酱 1.25 杯柠檬汁 5瓣大蒜 10汤匙橄榄油 2.5茶匙孜然

      【讨论】:

        猜你喜欢
        • 2015-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-09
        • 1970-01-01
        • 1970-01-01
        • 2016-08-25
        相关资源
        最近更新 更多