Skip to content Skip to sidebar Skip to footer

How To Use Function(1)(2) In Javascript? And How Does It Work?

I understand calling function(1) but not function(1)(2), how does it work? also possible for function(1)(2)(3)(4) too?

Solution 1:

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:

functionsum(a) {
    returnfunction(b) {
        return a+b;
    }
}

// Usage:window.alert(sum(5)(3));         // shows 8var add2 = sum(2);
window.alert(add2(5));           // shows 7window.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 6window.alert(add4(3)); // shows 7

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

var a = "Hello, world";

functionmultiply(a) {
    returnfunction(b) {
        return a * b;
    }
}

window.alert(multiply(6)(7)); // shows 42var 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.

Solution 2:

You have a function that returns a function:

functionf(n) {
  returnfunction(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:

functionf(n) {
  returnfunction(x) {
    returnfunction(y) {
      returnfunction(z) {
        return n + x + y + z;
      }
    }
  };
}

Solution 3:

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

x = f(1)(2)

is equivalent to:

f2 = f(1)
x = f2(2)

Solution 4:

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:

functionadd(n1) {
    returnfunctionadd_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.

Post a Comment for "How To Use Function(1)(2) In Javascript? And How Does It Work?"