admin管理员组文章数量:1134247
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?
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?
Share Improve this question edited Aug 14, 2013 at 14:32 M Abbas 6,4794 gold badges35 silver badges48 bronze badges asked Aug 14, 2013 at 14:28 L105L105 5,4193 gold badges19 silver badges23 bronze badges 3- 8 Personally I find this syntax confusing / awkward to read as well. But you can use simpler syntax to access AngularJS filters as described here: stackoverflow.com/a/14303362/1418796 – pkozlowski.opensource Commented Aug 14, 2013 at 14:41
- I took angularjs as an exemple. I wanted to know how to handle this if I create a function myself. – L105 Commented Aug 14, 2013 at 14:45
- 7 Actually it's called "currying". a programming technique. – Sajuuk Commented Jul 26, 2018 at 11:52
4 Answers
Reset to default 404It 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
$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')
It is the same as this:
var func = $filter('number');
func(number[, fractionSize]);
The $filter()
function returns a pointer to another function.
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
本文标签: javascriptTwo sets of parentheses after function callStack Overflow
版权声明:本文标题:javascript - Two sets of parentheses after function call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736772258a1952149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论