0

I've just started to learn javascript and come across one task that I don't understand. Can anyone explain me why do we return function name "return f" in this example and what for do we use "f.toString"?

function sum(a) {

  var currentSum = a;

  function f(b) {
    currentSum += b;
    return f;
  }

  f.toString = function() {
    return currentSum;
  };

  return f;
}

alert( sum(1)(2) ); // 3
alert( sum(5)(-1)(2) ); // 6
alert( sum(6)(-1)(-2)(-3) ); // 0
alert( sum(0)(1)(2)(3)(4)(5) ); // 15
3
  • 6
    You're not returning the "name", you're returning the function. Commented Jul 18, 2017 at 7:46
  • As @deceze says, you return function f, which on its turn returns f, after it added b to currentSum Commented Jul 18, 2017 at 7:48
  • As for the second question f.toString is called by alert (when it receives it as an argument) Commented Jul 18, 2017 at 7:49

5 Answers 5

1

Lets start with a simplified version:

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

It willwork much like yours, you can do:

sum(1)(2)(3);//f
sum(1)(2);//f

However, they always return a function, so were not able to access the currentSum. Its in sums scope and as its never returned or passed, its impossible to get it. So we probably need another function we can call to get the current sum:

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

  f.getSum = function() {
     return currentSum;
  };

  return f;
}

So now f has a property (functions are objects too), which is a function called getSum to get our variable

sum(1)(2)(3)//f
sum(1)(2)(3).getSum()//6 <= wohhooo

But why do we call it toString ?

When adding a variable to a string, its converted to a string, e.g.

1+"one"//"1one"

the same applies to objects/functions, but in this case, the js parser tries to call their toString method first, so:

sum(1)(2)+"three"

equals

sum(1)(2).toString()+"three"

the same conversion happens when passing non strings to alert.

Sign up to request clarification or add additional context in comments.

Comments

1

You are returning the object. So you can call the function again and, in the second call, you will have a currentSum allready.

when you do sum(3) the function will hold the number 3 and when you call it again doing sum(3)(4) it will add 4 to the currentSum.

then the alert will call the toString method and it will print the sum

Comments

1

look at the first example

alert( sum(1)(2) ); // 3
sum(1) // will return f. sum must return a function in order for the syntax to work

after it will return f it will become:

alert( f(2) );

Comments

1

With function sum you are passing the first argument a and returning function f which sum currentSum with the argument passed to f (b) and return again f and you can do that many times you want at the end it will call alert function which needs its first argument to be string. For that purpose you have rewritten the method on function f toString which in this case will return the currentSum.

Every object has a toString() method that is automatically called when the object is to be represented as a text value.

Comments

0

This is the task for understanding the type system of JavaScript. The task is to make function that adds numbers using currying. The idea is to make function that adds numbers when called like this sum(1)(2)(3), as you written in the task.

What do we do (looking to your code) in function sum:

1.

  var currentSum = a;

Here you declare a sum variable in scope of function sum.

  1. function f(b) { currentSum += b; return f; }

Then you declare the function that will perform summation. It returns itself for possibility of doing such thing: f(1)(2)(3)

3.

  f.toString = function() {
     return currentSum;
  };

After that you declare that f, converted to string returns sum value

  1. Than you return f to start adding.

Than what about f.toString - it 's being called, when f is passed alert as an argument. That's automatic casting of javascript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.