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
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Symbol.hasInstance' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262)The definition of 'Symbol.hasInstance' in that specification. | Draft | |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 51 | (Yes) | 50 (50) | No support | ? | ? |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | ? | (Yes) | 50.0 (50) | No support | ? | ? |