views:

168

answers:

4

I understand calling function(1) but not function(1)(2), how does it work?

also possible for function(1)(2)(3)(4) too?

+12  A: 

In this case you are supposing that function(1) returns a function, than you are calling this new, anonymous function with an argument of 2.

See this example:

function sum(a) {
    return function(b) {
        return a+b;
    }
}

// Usage:
window.alert(sum(5)(3));         // shows 8

var add2 = sum(2);
window.alert(add2(5));           // shows 7
window.alert(typeof(add2));      // shows 'function'

Here we create a function sum that takes one argument. Inside the function sum, we create an anonymous function that takes another argument. This anonymous function is returned as the result of executing sum.

Note that this anonymous function is a great example of what we call closure. A closure is a function that keeps the context in which it was created. In this case, it will keep the value of the variable a inside it, as did the example function add2. If we create many closures, they are independent as you can see:

var add3 = sum(3);
var add4 = sum(4);

window.alert(add3(3)); // shows 6
window.alert(add4(3)); // shows 7

Furthermore, they won't get "confused" if you have similarly named local variables:

var a = "Hello, world";

function multiply(a) {
    return function(b) {
        return a * b;
    }
}

window.alert(multiply(6)(7)); // shows 42

var twoTimes = multiply(2);
window.alert(typeof(twoTimes));
window.alert(twoTimes(5));

So, after a call to sum(2) or multiply(2) the result is not a number, nor a string, but is a function. This is a characteristic of functional languages -- languages in which functions can be passed as parameters and returned as results of other functions.

Bruno Reis
+1 for great link to closures
bguiz
+1  A: 

If your function returns a function, you can call that too.

x = f(1)(2)

is equivalent to:

f2 = f(1)
x = f2(2)
Mark Byers
+1  A: 

The parenthesis indicate invocation of a function (you "call" it). If you have

<anything>()

It means that the value of anything is a callable value. Imagine the following function:

function add(n1) {
    return function add_second(n2) { 
        return n1+n2
    }
}

You can then invoke it as add(1)(2) which would equal 3. You can naturally extend this as much as you want.

Svend
No need to give a name to the function being returned, btw. `return function(n2)` would be just as good, and less typing, too.
Chris Charabaruk
I know. I felt maybe it would be better to not muddy the issue at hand by also using anonymous functions.
Svend
+4  A: 

You have a function that returns a function:

function f(n) {
  return function(x) {
    return n + x;
  };
}

When you call f(1) you get a reference to a function back. You can either store the reference in a variable and call it:

var fx = f(1);
var result = fx(2);

Or you can call it directly:

var result = f(1)(2);

To get a function that returns a function that returns a function that returns a function, you just have to repeat the process:

function f(n) {
  return function(x) {
    return function(y) {
      return function(z) {
        return n + x + y + z;
      }
    }
  };
}
Guffa
Curious about the downvote? If you don't explain what it is that you don't like, it's rather pointless...
Guffa