如何检查 Javascript 类是否继承了另一个类(不创建 obj)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14486110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:57:54  来源:igfitidea点击:

How to check if a Javascript Class inherits another (without creating an obj)?

javascriptclassinheritance

提问by Simon

Eg:

例如:

function A(){}
function B(){}
B.prototype = new A();

How can I check if the class B inherits class A?

如何检查B类是否继承了A类?

回答by Nirvana Tikku

Try the following:

请尝试以下操作:

ChildClass.prototype instanceof ParentClass

回答by Denys Séguret

You can test direct inheritance with

您可以测试直接继承

B.prototype.constructor === A

To test indirect inheritance, you may use:

要测试间接继承,您可以使用:

B.prototype instanceof A

(this second solution was first given by Nirvana Tikku)

(这第二个解决方案首先由 Nirvana Tikku 给出)

回答by pery mimon

back to 2017:
check if that work for you

回到 2017 年:
检查这是否适合您

ParentClass.isPrototypeOf(ChildClass)

回答by jlchereau

I do not think Simon meant B.prototype = new A()in his question, because this is certainly not the way to chain prototypes in JavaScript.

我不认为 SimonB.prototype = new A()在他的问题中的意思,因为这肯定不是在 JavaScript 中链接原型的方式。

Assuming B extends A, use Object.prototype.isPrototypeOf.call(A.prototype, B.prototype)

假设 B 扩展 A,使用 Object.prototype.isPrototypeOf.call(A.prototype, B.prototype)

回答by Pacerier

Gotchas:Note that instanceofdoes not work as expected if you use multiple execution contexts/windows. See §§.

问题:请注意,instanceof如果您使用多个执行上下文/窗口,则不会按预期工作。见§§



Also, per https://johnresig.com/blog/objectgetprototypeof/, this is an alternative implementation that is identical to instanceof:

此外,根据https://johnresig.com/blog/objectgetprototypeof/,这是一个与以下相同的替代实现instanceof

function f(_, C) { // instanceof Polyfill
  while (_ != null) {
    if (_ == C.prototype)
      return true;
    _ = _.__proto__;
  }
  return false;
}

Modifying it to check the class directly gives us:

修改它以直接检查类给我们:

function f(ChildClass, ParentClass) {
  _ = ChildClass.prototype;
  while (_ != null) {
    if (_ == C.prototype)
      return true;
    _ = _.__proto__;
  }
  return false;
}



Sidenote边注

instanceofitself checks if obj.protois f.prototype, thus:

instanceof本身检查是否obj.protof.prototype,因此:

function A(){};
A.prototype = Array.prototype;
[]instanceof Array // true

and:

和:

function A(){}
_ = new A();
// then change prototype:
A.prototype = [];
/*false:*/ _ instanceof A
// then change back:
A.prototype = _.__proto__
_ instanceof A //true

and:

和:

function A(){}; function B(){};
B.prototype=Object.prototype;
/*true:*/ new A()instanceof B 

If it's not equal, proto is swapped with proto of proto in the check, then proto of proto of proto, and so on. Thus:

如果不相等,则 proto 在检查中与 proto 的 proto 交换,然后是 proto of proto of proto 的 proto,依此类推。因此:

function A(){}; _ = new A()
_.__proto__.__proto__ = Array.prototype
g instanceof Array //true

and:

和:

function A(){}
A.prototype.__proto__ = Array.prototype
g instanceof Array //true

and:

和:

f=()=>{};
f.prototype=Element.prototype
document.documentElement instanceof f //true
document.documentElement.__proto__.__proto__=[];
document.documentElement instanceof f //false