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

134
Vistas
Differentiate Class Constructor from "Normal" Function

If I have two variable names A and b, while A is a class constructor and b is a function, how do I write a function that takes the name as parameter and distinguishes whether it is a class constructor or not?

Or to put it in another way, how do I know if I have to call the function with the new operator or not?

class A{
   constructor(){
   }
}

function b(){
}

isClass(arg:(new ()=>A) | Function):boolean{
   // some code that returns true if arg is a class
}
isClass(b) // false
isClass(A) // true

There is one solution I came up with:

arg.toString().startsWith("class")

This works because arg.toString() returns the definition of arg, in this example something like class A{constructor(){}} for A and function b(){} for b. My solution works but I can imagine there being something neater than that.

Using the typeof operator returns "function" for both A and b, and arg.prototype is only undefined if b is an arrow function.

about 4 years ago · Juan Pablo Isaza
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