javascript - Two sets of parentheses after function call

ID : 20407

viewed : 16

Tags : javascriptangularjsjavascript

Top 5 Answer for javascript - Two sets of parentheses after function call

vote vote

95

It means that the first function ($filter) returns another function and then that returned function is called immediately. For Example:

function add(x){   return function(y){     return x + y;   }; }  var addTwo = add(2);  addTwo(4) === 6; // true add(3)(4) === 7; // true 
vote vote

80

$filter('number') returns a function that accepts two arguments, the first being required (a number) and the second one being optional (the fraction size).

It's possible to immediately call the returned function:

$filter('number')('123') 

Alternatively, you may keep the returned function for future use:

var numberFilter = $filter('number');  numberFilter('123') 
vote vote

80

It is the same as this:

var func = $filter('number'); func(number[, fractionSize]); 

The $filter() function returns a pointer to another function.

vote vote

67

with ES6 or later versions you can do it that way;

const divideBoth = (x) => (y) => {    return x / y; }; 

one of the reasons that makes this function type useful is when you have a react.js component that needs have callback function instead of doing it inline way(which is ()=>return value) you can do it the way we did previously. But it is not recommended to use in event callbacks because it gets execute in the first render which might cause issues sometimes

vote vote

58

Top 3 video Explaining javascript - Two sets of parentheses after function call

Related QUESTION?