Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
How does the console.log determine that an object is a custom (not built-in) class or instance of custom class?

Let me explain my question with an example. Let's define some class and its instance:

class MyClass {
  a() { return 1 }
}

const myInstance = new MyClass()

console.log(MyClass) returns "[class MyClass]".

console.log(myInstance) returns "MyClass {}"

Where does console.log get these values from? I tried several ways to get it, but they return different:

For the class:

console.log(MyClass.toString()) // class MyClass {
                                //   a() { return 1; }
                                // }

console.log(MyClass.constuctor.toString()) // function Function() { [native code] }

console.log(MyClass.constuctor.name) // Function

console.log(Object.prototype.toString.call(MyClass)) // [object Function]

console.log(MyClass[Symbol.toStringTag]) // undefined

And for the instance:

console.log(myInstance.toString()) // [object Object]

console.log(myInstance.constuctor.toString()) // class MyClass {
                                              //   a() { return 1; }
                                              // }


console.log(myInstance.constuctor.name) // MyClass

console.log(Object.prototype.toString.call(MyClass)) // [object Object]

console.log(myInstance[Symbol.toStringTag]) // undefined

So, let's imagine that my goal is to write a function that returns "class MyClass" when passing a class, and returns "MyClass {}" when passing an instance. How can I do this? Of course, similar data is in the results of the examples above and I can use it. However, I suspect there is a much simpler solution.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!