How do I write an arrow function in ES6 recursively?

后端 未结 12 893
南旧
南旧 2020-12-08 06:36

Arrow functions in ES6 do not have an arguments property and therefore arguments.callee will not work and would anyway not work in strict mode even

12条回答
  •  爱一瞬间的悲伤
    2020-12-08 07:17

    This is a version of this answer, https://stackoverflow.com/a/3903334/689223, with arrow functions.

    You can use the U or the Y combinator. Y combinator being the simplest to use.

    U combinator, with this you have to keep passing the function: const U = f => f(f) U(selfFn => arg => selfFn(selfFn)('to infinity and beyond'))

    Y combinator, with this you don't have to keep passing the function: const Y = gen => U(f => gen((...args) => f(f)(...args))) Y(selfFn => arg => selfFn('to infinity and beyond'))

提交回复
热议问题