Closure in Javascript with multiple brackets

前端 未结 5 590
梦谈多话
梦谈多话 2021-02-01 21:00

Could any one explain how this function alert, when more no of brackets of parameters are passed. I could not able to understand it clearly.

function sum(a) {

          


        
5条回答
  •  深忆病人
    2021-02-01 21:57

    The sum and f functions always return the f function, you can invoke it infinite times without getting a result other than the function.

    The value is only returned by the overwritten toString method of f (which actually returns a number):

    console.log( sum(1)(2) ) // Function(){}
    console.log( sum(1)(2).toString() ) // 3
    

    The alert function implicitly invokes the toString method when it casts its arguments to strings.

提交回复
热议问题