How to understand trampoline in JavaScript?

前端 未结 3 1222
面向向阳花
面向向阳花 2020-12-12 11:30

Here is the code:

function repeat(operation, num) {
  return function() {
    if (num <= 0) return
    operation()
    return repeat(operation, --num)
  }         


        
3条回答
  •  孤街浪徒
    2020-12-12 11:36

    The while loop will keep running until the condition is falsy.

    fn && typeof fn === 'function' will be falsy either if fn itself is falsy, or if fn is anything other than a function.

    The first half is actually redundant, since falsy values are also not functions.

提交回复
热议问题