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

194
Views
How to check if a function is of given class type in javascript?

I want to be able to check if a function is of a given class type in javascript. For example, let's say I have two classes:

class Horse {}
class Chicken {}

And let's say I want to create a function that will tell me if a function passed is Horse, something like that:

function isHorseClass(func) {
    // check if func is of Horse class type
    return isHorse;
}

The function will be called in the following way:

isHorseClass(Horse) // should return true
isHorseClass(Chicken) // should return false

Notice that the class is passed dynamically without instantiating the object of a class, so I cannot use instanceof to check the type here. Is there a way to check the type of the class dynamically, like in the example above?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Just create an instance and check using the instanceof operator.

class Horse {}
class Dog {}
class SubHorse extends Horse {}

function isHorseClass(Cls) {
  const instance = new Cls();
  return instance instanceof Horse;
}

console.log(isHorseClass(Horse));
console.log(isHorseClass(Dog));
console.log(isHorseClass(SubHorse));

The above answer returns true even for subclasses of Horse, if you just want an exact match use const isHorseClass = () => Cls === Horse.

about 4 years ago · Juan Pablo Isaza Report

0

For an exact match, you can just use ===:

function isHorseClass(func) {
    return func === Horse;
}

function isHorseClass(func) {
    return func === Horse;
}

class Horse {}
class Chicken {}

console.log("Horse", isHorseClass(Horse));     // true
console.log("Chicken", isHorseClass(Chicken)); // false

...but if you want to also get true for Horse subclasses, then that's possible with class syntax (not as much with the older ES5 syntax, but keep reading). You can do this:

function isHorseClass(func) {
    while (func && func !== Function.prototype) {
        if (func === Horse) {
            return true;
        }
        func = Object.getPrototypeOf(func);
    }
    return false;
}

That works because class syntax sets up two inheritance lines: one for the prototype assigned to instances, and a different one for the constructor functions themselves. For instance:

class Horse {}
class Thoroughbred extends Horse {}

That creates these two chains:

Thoroughbred.prototype −−−−> Horse.prototype −−−−> Object.prototype
Thoroughbred           −−−−> Horse            −−−> Function.prototype

This is fairly unique to JavaScript. :-)

Live Example:

function isHorseClass(func) {
    while (func && func !== Function.prototype) {
        if (func === Horse) {
            return true;
        }
        func = Object.getPrototypeOf(func);
    }
    return false;
}

class Horse {}
class Thoroughbred extends Horse {}
class Chicken {}

console.log("Horse", isHorseClass(Horse));               // true
console.log("Thoroughbred", isHorseClass(Thoroughbred)); // true
console.log("Chicken", isHorseClass(Chicken));           // false

With ES5, though (including standard versions of how class syntax is transpiled to ES5), you can't do that because they usually don't set up the constructor function inheritance (because you couldn't without ES2015+ featuers). You can get close by checking just the prototype chain, though. Combining that with the earlier version:

function isHorseClass(func) {
    while (func && func !== Function.prototype) {
        if (func === Horse || func.prototype === Horse.prototype) {
            return true;
        }
        func = Object.getPrototypeOf(func);
    }
    return false;
}

That will produce false positives. For instance, if I did this:

function Horse() {
}
function Unrelated() {
}
Unrelated.prototype = Horse.prototype;

...it would produce a false positive for Unrelated.

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