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
.