skip to Main Content

enter image description here

Just like the one displayed in the image. I am trying to know what does "({})" stand for and what’s the specific meaning for it.

Page({     // Here

})

Looked for it up in search engines, no useful results showed up.

4

Answers


  1. {} in JavaScript is an object literal:

    let obj = {
        // properties go here
        // can be empty
    };
    
    Login or Signup to reply.
  2. 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});
    
    Login or Signup to reply.
  3. In order to understand what ({}) means, you need to understand what {} and () might mean.

    {}:

    • object literal: {a: 1, b: 2}, in which case an object is defined/created
    • function block: function() {} or () => {} which specifies a function, but this is not your case, because this would have something else besides the {}
    • etc. (code block, substring, like "({})")

    ():

    • function parameter list, like foo(param1, param2)
    • paranthesis operator, like (a + b) * c

    Now, your example is clearly a function call and an object literal being passed to that function. Example:

    function foo(obj) {
        console.log(obj);
    }
    
    foo({a: 1, b: 2, c: 3})

    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.

    Login or Signup to reply.
  4. () 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search