Check if a constructor inherits another in ES6
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a situation where I need to check if a constructor (X) has another constructor (Y) in its prototype chain (or is Y itself). The quickest means to do this might be (new X()) instanceof Y . That isn't an option in this case because the constructors in question may throw if instantiated without valid arguments. The next approach I've considered is this: const doesInherit = ( A , B ) => { while ( A ) { if ( A === B ) return true ; A = Object . getPrototypeOf ( A ); } return false ; } That works, but I can't shake the sense that