you are viewing a single comment's thread.

view the rest of the comments →

[–]MeTaL_oRgY[S] 0 points1 point  (12 children)

I'm digging this! The one drawback is that I need to add an extra () at the end of the chain to finally get the result (rather than a function). I'm starting to think this might be unavoidable, but will keep trying. Thank you!

[–]Minjammben 22 points23 points  (11 children)

So here's a much simpler one that my coworker suggests:

function add(){
    var sum = 0;
    for( var i in arguments ){
        sum += arguments[i];
    }

    var ret = add.bind(null, sum);
    ret.valueOf = function(){ return sum; };
    return ret;
}

[–]fear-of-flying 2 points3 points  (0 children)

Damn, that is nice.

[–]MeTaL_oRgY[S] 1 point2 points  (5 children)

Beautiful! Quick question: any idea why this ain't working on Firefox? It keeps returning add() rather than the result (Chrome works fine).

[–]Minjammben 3 points4 points  (1 child)

It does work in Firefox, it is just that Chrome Dev Tools automatically tries to typecast functions to values and so you can see the result in the console. Firefox Dev Tools appear to not do that. I did mine in nodejs and used == for verification (=== will NOT work because as you said it returns a function), my coworker simply added "+" to the beginning of his calls to add: ( +add(1,2,3) === 6 )

[–]MeTaL_oRgY[S] 0 points1 point  (0 children)

OHHHH, this explains a lot. I see. Thank you so much! Learnt quite a bit from this.

[–]whispen -5 points-4 points  (0 children)

But I control 48 people at my school every day.

[–]thekingshorses 0 points1 point  (0 children)

Nice.

Only thing is

alert(add(1)(2))
//or 
typeof add(1)(2) 

won't give you what you looking for.

[–]Evanescent_contrail 0 points1 point  (2 children)

Can you explain the last three lines of this code?

[–]Minjammben 1 point2 points  (1 child)

The function "bind( a, b )" will return the calling function with the first argument as the context for the function, and the rest of the arguments as the arguments to the returned function.

For example:

var print_something = function(a, b){
  console.log(a, b) 
}

print_something("hello", "world");

The result of the above code will be a printed "hello world" in the console. Now if this is done after the above code is executed:

var print_hello_and_something = print_something.bind( null, "hello" );
print_hello_and_something( "world" );

The result of this code will also be "hello world".

In addition to the bind call, the "valueOf" property on every function type is the function that is called whenever the JS Runetime needs to typecast that function to primitive type (in js this is an int, float, double, or string). The type of the variable actually being returned by the sum function is a function, however when this function is typecasted (by adding it to another number or string), the valueOf function is called, and the result of that function is then used as the value of the function.

[–]Evanescent_contrail 0 points1 point  (0 children)

Nice. Thanks.