How does redefining function's toString method work?

Viewed 28

I am trying to solve a code challenge where I came across a challenge to chain functions and most of the solutions i could find online were using function.toString and override toString method of function like in the following code to chain add function.

function sum(n) {
  var v = function(x) {
    return sum(n + x);
  };

  v.toString = function() {
    return n;
  };

  return v;
}

What does toString do here to replace function call method? and how does toString work here without a bracket like toString()

1 Answers

var result

function v(n,x){
result = (n+x).toString()
}

// This function return sum of  two parameters as a result in string type. toString() functions allows you to parse anything into String. 

Related