[英]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:我希望能够检查 function 是否属于 javascript 中给定的 class 类型。例如,假设我有两个类:
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,它将告诉我通过的 function 是否是 Horse,类似这样:
function isHorseClass(func) {
// check if func is of Horse class type
return isHorse;
}
The function will be called in the following way: function 将按以下方式调用:
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.请注意,class 是动态传递的,而没有实例化 class 的 object,因此我无法在此处使用
instanceof
来检查类型。 Is there a way to check the type of the class dynamically, like in the example above?有没有办法像上面的例子那样动态地检查 class 的类型?
Just create an instance and check using the instanceof
operator.只需创建一个实例并使用
instanceof
运算符进行检查。
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
.上面的答案即使对于
Horse
的子类也会返回 true ,如果您只想完全匹配,请使用const isHorseClass = () => Cls === Horse
。
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). ...但是,如果您还想为
Horse
子类true
,那么可以使用class
语法(与旧的 ES5 语法不同,但请继续阅读)。 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.这是有效的,因为
class
语法设置了两行 inheritance :一行用于分配给实例的原型,另一行用于构造函数本身。 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. :-)这对于 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).但是,对于 ES5(包括如何
class
语法转换为 ES5 的标准版本),您不能这样做,因为它们通常不会设置构造函数 function inheritance(因为没有 ES2015+ 特性就无法做到)。 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
. ...它会对
Unrelated
产生误报。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.