V
V
Vitali Kozlov2015-09-28 14:56:40
JavaScript
Vitali Kozlov, 2015-09-28 14:56:40

How to implement multiple function call in JavaScript?

Is it realistic to get such a result or not? If yes, how?
console.log(add(1)(3)(5)(35)); // 44
There can be any number of calls.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Zuev, 2015-09-28
@DraX

Sum of arbitrary number of brackets

L
Lenar Fattakhov, 2015-09-28
@fr_end

read about currying
sekrasoft.livejournal.com/60548.html

I
Ivanq, 2015-09-28
@Ivanq

Unreal.

M
Mikhail Osher, 2015-09-28
@miraage

Here are some of the examples.
As far as I remember, it is necessary not toString, but valueOf to assign.
Check both options.

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question