有没有instanceof
的静态类似功能?即,而不是:
obj1 instanceof Type
类似于:
TypeA instanceof TypeB
?
我在这件事上找不到任何东西,所以我黑了一下:
function InstanceOf(type,parent) {
do{ if(type === parent) return true;}
while(type = Object.getPrototypeOf(type));
return false;
}
//...
Instanceof(ChildClass, ParentClass); // = boolean
但我觉得有一个更好/更本土的方式来做到这一点,但再次,找不到任何东西.
有没有更好的/标准的方法来判断静态类继承?还是说我的黑客攻击是最好的?
Update
所以,我很好奇,对我破解的版本和公认答案提供的版本都进行了性能测试,getPrototypeOf
版本在运行getPrototypeOf
,000次时大约快了一倍.
另外,不,我不能发布测试代码,我是在 node REPL中发布的