How to achieve arbitrary chain on function call in javascript?

Huibin Zhang

I have written code to achieve

sum(1)(2) //3

the code looks like:

function sum(a) {

  return function(b) { 
    return a+b
 }

}

But I didn't work out the second question, which is how to achieve any arbitrary number of chain function call like:

sum(1)(2) == 3
sum(5)(-1)(2) == 6
sum(6)(-1)(-2)(-3) == 0
sum(0)(1)(2)(3)(4)(5) == 15
1983

Normally you'd do something like this:

var add = function(a,b){
    return a+b;
};

var sum = function(){
    return [].reduce.call(arguments, add);
}

And then you can write:

sum(1,2,3,4); // 10

But it is possible to hack the functionality you're after:

var sum = function(x){
    var f = function(y){
        return sum(x+y);
    };
    f.valueOf = function(){
        return x;
    };
    return f;
};

sum(1)(2)(3)(4); // 10

Just don't do it in production code please!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to chain a function call in D3?

From Dev

How to write a self chain function in Javascript

From Dev

How to call a function from a chain of module.exports in node

From Dev

Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call

From Dev

How do you curry any javascript function of arbitrary arity?

From Dev

How to call JavaScript function in VBA?

From Dev

How to call the jquery function in JavaScript?

From Dev

How to call nested function in javascript?

From Dev

How to call nested function in javascript?

From Dev

How to call JavaScript function in VBA?

From Dev

How to call a recursive function in javascript

From Dev

cancelling an asynchronous call chain in javascript

From Dev

Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

From Dev

How to call php function from JavaScript function?

From Dev

How to call a function in a function with javascript small explanation

From Dev

How to call a function within another function in javascript

From Dev

How to call a function inside a main function in javascript

From Dev

how to call javascript function from SmartGWT function

From Dev

How to call a function in a function with javascript small explanation

From Dev

How to call a Server Side Function in a javascript Function

From Dev

How to call a function within another function in javascript

From Dev

How to call a function that is inside another function javascript

From Dev

How to call a function inside a main function in javascript

From Dev

Recursive function in javascript - how gets resovled in scope chain

From Dev

Javascript: How to call javascript function in html

From Dev

How to achieve stratified K fold splitting for arbitrary number of categorical variables?

From Dev

How do I step into arbitrary generator function call from pdb console?

From Dev

How to call this promise chain right

From Dev

How to modify place with arbitrary function

Related Related

  1. 1

    How to chain a function call in D3?

  2. 2

    How to write a self chain function in Javascript

  3. 3

    How to call a function from a chain of module.exports in node

  4. 4

    Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call

  5. 5

    How do you curry any javascript function of arbitrary arity?

  6. 6

    How to call JavaScript function in VBA?

  7. 7

    How to call the jquery function in JavaScript?

  8. 8

    How to call nested function in javascript?

  9. 9

    How to call nested function in javascript?

  10. 10

    How to call JavaScript function in VBA?

  11. 11

    How to call a recursive function in javascript

  12. 12

    cancelling an asynchronous call chain in javascript

  13. 13

    Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

  14. 14

    How to call php function from JavaScript function?

  15. 15

    How to call a function in a function with javascript small explanation

  16. 16

    How to call a function within another function in javascript

  17. 17

    How to call a function inside a main function in javascript

  18. 18

    how to call javascript function from SmartGWT function

  19. 19

    How to call a function in a function with javascript small explanation

  20. 20

    How to call a Server Side Function in a javascript Function

  21. 21

    How to call a function within another function in javascript

  22. 22

    How to call a function that is inside another function javascript

  23. 23

    How to call a function inside a main function in javascript

  24. 24

    Recursive function in javascript - how gets resovled in scope chain

  25. 25

    Javascript: How to call javascript function in html

  26. 26

    How to achieve stratified K fold splitting for arbitrary number of categorical variables?

  27. 27

    How do I step into arbitrary generator function call from pdb console?

  28. 28

    How to call this promise chain right

  29. 29

    How to modify place with arbitrary function

HotTag

Archive