Page() is a call of a function named Page with possible arguments between parenthesis.
{} is an empty object literal.
So the Page({}) is a call with 1 argument being an empty object passed to a function being called.
But we have another context where we could encounter ({}).
{} could be also a block of statements inside the parenthesis, for example { const test = true }. A block his own scope where we can declare variables belonging to the block only. We use blocks as function bodies also. In an arrow function we have an interesting case:
() => {} – basically that’s an arrow function with an empty block/body. But an arrow function could be bare (without a block) so in that case it returns the result of the expression after =>. Since {} is reserved for a function body, to return an object as an expression we should use:
() => ({}).
Another interesting case when should use () with {} to avoid {} being interpreted as a block is object assignment destructuring:
let prop1, prop2;
({prop1, prop2} = {prop1: 1, prop2:2});
Here we can see that we have a function that takes a parameter and console-logs it out. We call this function, i.e., execute it and pass an object to it, so the object is logged to the console via the function.
() has two meanings in Javascrip, one is function call, and the other is grouping. From this point of view, it should mean grouping, like this (1+2)x3.If it is a function, then the Function keyword needs to be used together.
{} can be represented as an object literal or a local scope. If it is a local scope, then some keywords need to be followed, such as for, function, etc. So this should be expressed as an object literal.
Combined, it should be a very high priority to create an anonymous object literal.
4
Answers
{}
in JavaScript is an object literal:Page()
is a call of a function namedPage
with possible arguments between parenthesis.{}
is an empty object literal.So the
Page({})
is a call with 1 argument being an empty object passed to a function being called.But we have another context where we could encounter
({})
.{}
could be also a block of statements inside the parenthesis, for example{ const test = true }
. A block his own scope where we can declare variables belonging to the block only. We use blocks as function bodies also. In an arrow function we have an interesting case:() => {}
– basically that’s an arrow function with an empty block/body. But an arrow function could be bare (without a block) so in that case it returns the result of the expression after=>
. Since{}
is reserved for a function body, to return an object as an expression we should use:() => ({})
.Another interesting case when should use
()
with{}
to avoid{}
being interpreted as a block is object assignment destructuring:In order to understand what
({})
means, you need to understand what{}
and()
might mean.{}
:{a: 1, b: 2}
, in which case an object is defined/createdfunction() {}
or() => {}
which specifies a function, but this is not your case, because this would have something else besides the{}
"({})"
)()
:foo(param1, param2)
(a + b) * c
Now, your example is clearly a function call and an object literal being passed to that function. Example:
Here we can see that we have a function that takes a parameter and console-logs it out. We call this function, i.e., execute it and pass an object to it, so the object is logged to the console via the function.
() has two meanings in Javascrip, one is function call, and the other is grouping. From this point of view, it should mean grouping, like this (1+2)x3.If it is a function, then the Function keyword needs to be used together.
{} can be represented as an object literal or a local scope. If it is a local scope, then some keywords need to be followed, such as for, function, etc. So this should be expressed as an object literal.
Combined, it should be a very high priority to create an anonymous object literal.