ES6 immediately invoke recursive arrow function

前端 未结 3 1477
-上瘾入骨i
-上瘾入骨i 2021-02-13 13:53

This is my current code:

const fn = parameter => {
    // if, else ...
    fn(X);
};
fn(0);

Now, I can\'t use this approach as I need to cal

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-13 13:55

    First, let me put the disclaimer that Immediately-Invoked-Function-Expressions (IIFE) are considered bad practice in ES6, and this is tail-recursion and personally I would change it to a for loop.

    but you can always do this I guess:

    ((x) =>{ const fn=(p)=>{
           //whatever
           fn(q)
       }
       fn(x)
    })(0)
    

提交回复
热议问题