Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda