在线文档教程

Symbol.hasInstance

Symbol.hasInstance

Symbol.hasInstance 用于判断某对象是否为某构造器的实例。 因此你可以用它自定义instanceof操作符在某个类上的行为。

| Symbol.hasInstance属性的属性特性 |

|:----|

| Writable | no |

| Enumerable | no |

| Configurable | no |

示例

例如,你可以这样来自定义 MyArray 的 instanceof 行为:

class MyArray { static [Symbol.hasInstance](instance) { return Array.isArray(instance } } console.log([] instanceof MyArray // true

规范

SpecificationStatusComment
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Symbol.hasInstance' in that specification.StandardInitial definition.
ECMAScript Latest Draft (ECMA-262)The definition of 'Symbol.hasInstance' in that specification.Draft

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support51(Yes)50 (50)No support??

FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support??(Yes)50.0 (50)No support??