Daniel Ortega
Daniel Ortega

Reputation: 115

When an object is printed in the node.js REPL, what does the identifier that appears before the opening curly brace mean?

Checking out the structure of the node.js googleapis library, I see the following:

> const google = require("googleapis")
> google
{ GoogleApis: [Function: GoogleApis],
  google:
   GoogleApis { 
     _discovery: Discovery { transporter: DefaultTransporter {}, options: [Object] },
     auth:
      AuthPlus {
        checkIsGCE: undefined,
        jsonContent: null,
        cachedCredential: null,
        _cachedProjectId: null,
        keyFilename: undefined,
        scopes: undefined,
        JWT: [Function: JWT],
        Compute: [Object],
        OAuth2: [Object] },
     _options: {} } }

And I don't understand the meaning of identifiers placed directly before an object literal. For instance, "GoogleApis", "Discovery" or "AuthPlus".

Apparently, they are not object keys:

> Object.keys(google)
[ 'GoogleApis', 'google' ]
> Object.keys(google.google)
[ '_discovery', 'auth', '_options' ]

They just appear right before the object literals:

> Object.entries(google.google)
[ [ '_discovery',
    Discovery { transporter: DefaultTransporter {}, options: [Object] } ],
  [ 'auth',
    AuthPlus {
      checkIsGCE: undefined,
      jsonContent: null,
      cachedCredential: null,
      _cachedProjectId: null,
      keyFilename: undefined,
      scopes: undefined,
      JWT: [Function: JWT],
      Compute: [Object],
      OAuth2: [Object] } ],
  [ '_options', {} ] ]

What is the meaning of an identifier appearing right before an object literal? eg:

"key": something {x:1, y:2, z:3}

(ie, "something" in my last example)

Upvotes: 1

Views: 61

Answers (3)

daemone
daemone

Reputation: 1191

Those are types. As @Quentin commented, what you're seeing is not actually JavaScript or JSON source code, but a pretty-print of the object meant for human consumption. Where type information is available for an object in the hierarchy, browser consoles and the Node REPL provide this information in the format you've seen.

You can see this for yourself pretty easily. Create a class (or a constructor function if you prefer) and then an object with an instance of that class inside it:

> class MyClass { constructor () {this.num = 23; } }
[Function: MyClass]
> const obj = { instance: new MyClass () }
undefined
> obj
{ instance: MyClass { num: 23 } }

As you can see, the output of the REPL tells you that the property instance is an instance of MyClass.

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 371019

It means that the object is an instantiation of the class whose name you're seeing. Try running this in your node:

class mySpecialClass {
  constructor() {
    this.myProp = 1;
  }
}
const myInstant = new mySpecialClass();

myInstant

mySpecialClass { myProp: 1 }

Upvotes: 1

vrajau
vrajau

Reputation: 116

It is the "class" of your object.

Upvotes: 0

Related Questions