检查构造函数是否继承了 ES6 中的另一个构造函数

Check if a constructor inherits another in ES6

我有一种情况需要检查构造函数 (X) 在其原型链中是否有另一个构造函数 (Y)(或者是 Y 本身)。

最快的方法可能是 (new X()) instanceof Y。在这种情况下,这不是一个选项,因为如果在没有有效参数的情况下实例化,有问题的构造函数可能会抛出。

我考虑的下一个方法是:

const doesInherit = (A, B) => {
  while (A) {
    if (A === B) return true;
    A = Object.getPrototypeOf(A);
  }

  return false;
}

这行得通,但我无法摆脱这样一种感觉,即我错过了一些更直接的方法来检查它。有吗?

由于 instanceof 的工作方式,您应该可以做到

A.prototype instanceof B

但这只会测试继承,你应该比较A === B来测试自引用:

A === B || A.prototype instanceof B

Babel example:

class A {}
class B extends A {}
class C extends B {}

console.log(C === C) // true
console.log(C.prototype instanceof B) // true
console.log(C.prototype instanceof A) // true

instanceof基本实现如下:

function instanceof(obj, Constr) {
  var proto;
  while ((proto = Object.getProtoypeOf(obj)) {
    if (proto === Constr.prototype) {
      return true;
    }
  }
  return false;
}

它遍历对象的原型链并检查是否有任何原型等于构造函数 prototype 属性.

几乎就像你所做的那样,但在内部。

还有Object.prototype.isPrototypeOf()。似乎是一个完美的用例,不是吗?

Babel

class A {}
class B extends A {}
class C extends B {}
console.log(C === C)
console.log(B.isPrototypeOf(C))
console.log(A.isPrototypeOf(C))

注意:上面使用 isPrototypeOf() 检查 ES6 类 A、B、C 继承链的答案效果很好。但是对于 ES6 之前的 类 对象、数组、函数等,它并不像您期望的那样工作

 Object.prototype.isPrototypeOf (Array.prototype); // => true 

但是:

 Object.isPrototypeOf (Array);   // => false

这是应该的。 Array 的 INSTANCES 继承了 Object.prototype 的方法。但是 "class" 数组不继承 "class" 对象。如果您向 Object 添加方法,则无法通过 Array 调用它。 Array 不继承 Object 的方法(如果有的话)。只是用户创建的 类 工作方式不同!

也许我们根本不应该将对象、数组和函数视为 "classes" - 即使您可以创建它们的实例。他们只有 "constructors"。或者我们可以说它们是 类 但 JavaScript 中的内置 类 与用户创建的不同。