javascript - How function unless works int the example in Chapter 5 Higher-Order Functions from the Eloquent JavaScript 3rd edition?
问题描述
The function repeat seems to take n = 0 as the argument and I don't know why. Can someone explain it to me, please?
function unless(test, then) {
if (!test) then();
}
repeat(3, n => {
unless(n % 2 == 1, () => {
console.log(n, "is even");
});
});
// → 0 is even
// → 2 is even
解决方案
You're passing two arguments to function unless one is Boolean
and another is function
unless(n % 2 == 1, () => {
console.log(n, "is even");
});
Here n % 2 == 1
is first argument and () => console.log(n, "is even"); }
is second
and in your function unless
function unless(test, then) {
if (!test) then();
}
We are first checking the if the test is false than only run the function passed as argument
推荐阅读
- xamarin - 为什么我的工具栏中的图标输出如此之小 xamarin 形式
- javascript - RethinkDb:在 .group() 之后无法在急切的流上调用“更改”
- javascript - 如何在调试 Chrome 扩展程序时查看来自 contentscript.js 的 debug() 消息
- python - Python:给定一个整数列表 x,编写一个表达式,如果所有奇数索引值都是其先前值的两倍,则返回 True
- python - VSCode Python with Git bash:运行程序后,尝试运行Activate.ps1
- python - 如何在django中获取同一列的两个值的差异
- laravel - 安装 Laravel 5.8 + Voyager 时 Composer 失败
- java - 将 1-N 数据从 Postman(或 HTML 文件)发送到 Jersey 服务
- azure - 如何在 azure 数据工厂中创建一个审核表,该表将保存在 Azure 数据工厂中运行的管道的状态
- scala - 优雅地将函数应用于 WrappedArray 的所有元素