What's The Second Set Of Parentheses Mean After A Require Statement In Node.js?
Solution 1:
Whatever is exported from ./tasks/clean
is a function, so it's just being invoked with 'js'
and './dist/js'
as parameters
It is equivalent to the following:
const clean = require('./tasks/clean');
clean('js', './dist/js');
Solution 2:
This syntax you are seeing is called Function currying, which is a popular technique in writing composable functions in the Functional programming paradigm. Currying and Functional programming are not new concepts, they have been around for decades, but Functional programming is starting to get really popular with the JavaScript community.
Currying basically lets you immediately invoke a function call from a function that returns a function.
Consider this function that returns a function:
functionfoo(x) {
console.log(x);
returnfunction(y) {
console.log(y);
}
}
now when calling this function, you can now do this:
foo(1)(2);
which will output to the console:
1
2
So in the example that you posted, the clean()
function must return a function that accepts two parameters, like this:
functionclean(a) {
returnfunction(b, c) {
console.log(a, b, c);
}
}
which allows it to be called like this:
clean('foo')('bar', 'baz');
//=> 'foo''bar''baz'
This was a super basic example, but I hope this helps!
Post a Comment for "What's The Second Set Of Parentheses Mean After A Require Statement In Node.js?"