Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda