Javascript add to string with each function call

后端 未结 5 2408
执念已碎
执念已碎 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 06:00

    Try this

    function f(input) {
      let str = 'f'
      var o = function(suffix){
        if(suffix)
          return str + suffix;
        str += "o";
        return o;
      }
      return o(input);
    }
    
    console.log(f('l'))
    console.log(f()('l'))
    console.log(f()()('l'))
    console.log(f()()()('l'))

提交回复
热议问题