• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • Symbol.hasInstance

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

    Symbol.hasInstance属性的属性特性:
    writablefalse
    enumerablefalse
    configurablefalse

    示例

    你可实现一个自定义的instanceof行为,例如:

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