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

129
Views
Get javascript object types as console.log outputs in node.js

I need to introspect some objects to know their type. In particular, I need to identify functions and async functions. For example consider the following object:

f = function(){}
af = async function(){}
class c {}

Using typeof won't work because for any of these objects, the type is function


> typeof f
'function'
> typeof af
'function'
> typeof c
'function'

This improves a little if I use Object.prototype.toString.call() on each object:

> Object.prototype.toString.call(f)
'[object Function]'
>Object.prototype.toString.call(af)
'[object AsyncFunction]'
>Object.prototype.toString.call(c)
'[object Function]'

I still, cannot differentiate a function from a class. However, console.log is able to do it:

> console.log(f)
[Function: f]
> console.log(af)
[AsyncFunction: af]
> console.log(c)
[class c]

So my question is, how can I mimic what console.log is doing to identify the correct type of a class?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use the util module of Node.js, for instance the inspect function.

Note: You need to import it const util = require("util"). It's not a global object!

> class A {}
> util.inspect(A)
'[class A]'
> f = () => {}
> af = async () => {}
> util.inspect(f)
'[Function: f]'
> util.inspect(af)
'[AsyncFunction: af]'
over 4 years ago · Santiago Trujillo Report
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!