Two sets of parentheses after function call
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
Two sets of parentheses in a JavaScript function call?
cookie-parser
module returns a function, which is getting called in the code you shared.
app.use(require('cookie-parser')(credentials.cookieSecret));
could be rewritten as:
var cookieParser = require('cookie-parser')
var cookieParserInstance = cookieParser(credentials.cookieSecret)
app.use(cookieParserInstance)
JS Function With Two Parentheses and Two Params
How could I alter that function so it could be run with one set of parameters, or two, and produce the same result?
You can almost do that, but I'm struggling to think of a good reason to.
Here's how: You detect how many arguments your function has received and, if it's received only one, you return a function instead of a number — and have that function add in the second number if it gets called:
function add(a,b) { if (arguments.length === 1) { return function(b2) { // You could call this arg `b` as well if you like, return a + b2; // it would shadow (hide, supercede) the one above }; } return a + b;}console.log(add(10, 10)); // 20console.log(add(10)(10)); // 20
Another set of parentheses after a function call [C++]
This is just some funny code to illustrate a point I guess.
[](int const n) { return n*2; }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This code defines an anonymous function (lambda). It is transformed by the compiler to a construct similar to this one:
struct {
operator()(int const n) { return n*2; }
};
Now compose()
takes this struct and packages it in another likewise struct that has a constructor and stores the struct above in a member variable.
You end up with a call operator()(4)
on an anonymous struct.
What's the second set of parentheses mean after a require statement in Node.js?
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');
C++ multiple sets of parameters with their own parentheses on function call?
options.add_options()
returns an object.
That object has the function call operator overload that takes two strings, which most likely looks like
ObjectType& operator()(std::string const& option, std::string const& value);
which allows you to chain the function calls.
Here's a simple program that demonstrates the concept.
#include <iostream>
struct Foo
{
Foo& operator()(int x)
{
std::cout << "Got " << x << std::endl;
return *this;
}
};
struct Bar
{
Foo getFoo() { return Foo(); }
};
int main()
{
Bar b;
b.getFoo()(10)(200)(30);
}
Output of the program:
Got 10
Got 200
Got 30
That line in main
is equivalent to:
Foo foo = b.getFoo();
foo(10);
foo(200);
foo(30);
PS
Personally, I find that style of coding a bit cryptic and best avoided. I would rather see:
auto& option = options.add_options();
option.addOption("option1", "Description1");
option.addOption("option2", "Description2");
That's a lot clearer to understand, IMO.
Function calling in Javascript with double brackets
The double parenthesis would have been useful if hi
had returned a function instead of its name, like in
function hi(){
return hello;
}
hi()();
That's probably what was the intent.
Related Topics
Using "Object.Create" Instead of "New"
Create a Custom Callback in JavaScript
Jquery How to Bind Onclick Event to Dynamically Added HTML Element
Create Object from Class Name in Javasscript Ecmascript 6
Most Efficient Way to Convert an HTMLcollection to an Array
Add Directives from Directive in Angularjs
Passing an Array as a Function Parameter in JavaScript
When Does Reflow Happen in a Dom Environment
How to Add a Custom Right-Click Menu to a Webpage
How to Open a Bootstrap Modal Window Using Jquery
JavaScript - Href VS Onclick for Callback Function on Hyperlink
Is Right Click a JavaScript Event
Promise.All Consumes All My Ram
Explanation of <Script Type = "Text/Template"> ... </Script>
How to Add External Js Scripts to Vuejs Components