Closure in Javascript with multiple brackets

前端 未结 5 644
梦谈多话
梦谈多话 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:53

    The thing to look at is this piece of code

    function f(b) {
        sum += b
        return f
      }
    

    This function returns reference to itself so it can be called as many times as possible. An important thing about it is that it has a tostring function that gets called and since tostring is defined inside function sum() it has access to the variable sum and its value current value (that is changed by f() )

提交回复
热议问题