首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Symbol.hasInstance

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

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

|:----|

| Writable | no |

| Enumerable | no |

| Configurable | no |

示例

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

代码语言:javascript
复制
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

?

?

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com