如何检查一个Javascript类是否inheritance另一个(不创build一个obj)?
例如:
function A(){} function B(){} B.prototype = new A(); 
如何检查B类是否inheritanceA类?
 给这个试一下B.prototype instanceof A 
你可以用直接inheritance来testing
 B.prototype.constructor === A 
要testing间接inheritance,你可以使用
 B.prototype instanceof A 
(这第二个解决scheme是由Nirvana Tikku首先提供的)
  问题:请注意,如果使用多个执行上下文/窗口, instanceof不能按预期工作。 见§§ 。 
 另外,根据https://johnresig.com/blog/objectgetprototypeof/ ,这是一个与instanceof相同的替代实现: 
 function f(_, C) { // instanceof Polyfill while (_ != null) { if (_ == C.prototype) return true; _ = _.__proto__; } return false; } 
修改它直接检查类给我们:
 function f(ChildClass, ParentClass) { _ = ChildClass.prototype; while (_ != null) { if (_ == C.prototype) return true; _ = _.__proto__; } return false; } 
边注
  instanceof本身检查obj.proto是否是f.prototype ,因此: 
 function A(){}; A.prototype = Array.prototype; []instanceof Array // true 
和:
 function A(){} _ = new A(); // then change prototype: A.prototype = []; /*false:*/ _ instanceof A // then change back: A.prototype = _.__proto__ _ instanceof A //true 
和:
 function A(){}; function B(){}; B.prototype=Object.prototype; /*true:*/ new A()instanceof B 
如果不相等,则将原始数据replace为原始数据的原始数据,然后是原型数据的原型数据,等等。 从而:
 function A(){}; _ = new A() _.__proto__.__proto__ = Array.prototype g instanceof Array //true 
和:
 function A(){} A.prototype.__proto__ = Array.prototype g instanceof Array //true 
和:
 f=()=>{}; f.prototype=Element.prototype document.documentElement instanceof f //true document.documentElement.__proto__.__proto__=[]; document.documentElement instanceof f //false 
 回到2017年: 
 检查是否为你工作 
 A.isPrototypeOf(B)