Javascript add to string with each function call

后端 未结 5 2369
执念已碎
执念已碎 2020-12-21 05:32

I have the following situation where I have a function f which takes an argument input.

I want to be able to have f such that

5条回答
  •  感情败类
    2020-12-21 05:59

    f() can not return fo, it has to return a function. To get fo you need to call f like f()('').

    Try this:

    function f(input, acc) {
        acc = acc || 'f'
        if (input === undefined) {
            return function(a) {
                return f(a, acc + 'o')
            }
        }
        return acc + input
    }
    
    console.log(f('l'))
    console.log(f()(''))
    console.log(f()('l'))
    console.log(f()()('foo'))

提交回复
热议问题