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

131
Views
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
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!