Function 雄辩的JavaScript书:don';我不理解这个例子
在第3章中,有一个鹰嘴豆泥配方的例子,我真的不明白是这样的代码:Function 雄辩的JavaScript书:don';我不理解这个例子,function,eloquent,console.log,progress,recipe,Function,Eloquent,Console.log,Progress,Recipe,在第3章中,有一个鹰嘴豆泥配方的例子,我真的不明白是这样的代码: const hummus = function(factor) { const ingredient = function(amount, unit, name) { let ingredientAmount = amount * factor; if (ingredientAmount > 1) { unit += "s"; } console.log(`
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,试图更好地跟踪进度,了解这里实际发生了什么,但我无法对此保持清醒。我的意思是这里的预期结果是什么?示例的目标是向您展示函数范围。通过将固定参数传递给hummus函数,您可以返回一些新的文本对象,这些对象将通过使用内部函数和外部函数参数因子来计算最终结果。只需添加hummus(amount)即可获得配料的输出 鹰嘴豆泥(5);在代码末尾调用函数 输出: 5罐鹰嘴豆 1.25杯塔希尼 1.25杯柠檬汁 5瓣大蒜 10汤匙橄榄油 2.5茶匙孜然