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?
CodePudding user response:
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
.
CodePudding user response:
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
.