Two sets of parentheses after function call

2019-01-01 01:44发布

问题:

I was looking how filters works in Angularjs and I saw that we need to send 2 sets of parentheses.

$filter(\'number\')(number[, fractionSize])

What does it means and how do we handle it with JavaScript?

回答1:

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


回答2:

$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\')


回答3:

It is the same as this:

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

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