console.log 当作为参数传递给其他函数并在内部调用时不是函数

console.log not a function when passed as an argument to some other function and called within

我正在尝试构建自己的 for 循环结构。

function loop(value, test, update, body) {
    if (test(value)) {
        body(value);
        return loop(update(value), test, update);
    }
}

Edge 浏览器给我的错误:

VM1297:3 Uncaught TypeError: body is not a function
    at loop (<anonymous>:3:3)
    at loop (<anonymous>:4:10)
    at <anonymous>:1:1

使用此调用:

loop(10, n => n > 0, n => n - 1, console.log)

为什么 JS 看不到 body 是一个函数?

您没有将正文作为下一次迭代的最后一个参数传递。 你应该做 loop(update(value), test, update, body); 而不是 loop(update(value), test, update);

function loop(value, test, update, body) {
  if (test(value)) {
    body(value);
    return loop(update(value), test, update, body);
  }
}

loop(10, n => n > 0, n => n - 1, console.log)