Function.prototype[Symbol.hasInstance]()

Function 实例的 [Symbol.hasInstance]() 方法指定用于确定构造函数是否将对象识别为构造函数实例之一的默认过程。它由 instanceof 运算符调用。

¥The [Symbol.hasInstance]() method of Function instances specifies the default procedure for determining if a constructor function recognizes an object as one of the constructor's instances. It is called by the instanceof operator.

语法

¥Syntax

js
func[Symbol.hasInstance](value)

参数

¥Parameters

value

要测试的对象。原始值始终返回 false

返回值

¥Return value

如果 func.prototypevalue 的原型链中,则 true;否则,false。如果 value 不是对象或 this 不是函数,则始终返回 false。如果 this绑定函数,则返回对 value 和底层目标函数进行 instanceof 测试的结果。

¥true if func.prototype is in the prototype chain of value; otherwise, false. Always returns false if value is not an object or this is not a function. If this is a bound function, returns the result of a instanceof test on value and the underlying target function.

例外情况

¥Exceptions

TypeError

如果 this 不是绑定函数并且 this.prototype 不是对象,则抛出该异常。

描述

¥Description

只要存在这样的方法,instanceof 运算符就会调用右侧的 [Symbol.hasInstance]() 方法。因为默认情况下所有函数都继承自 Function.prototype,所以它们都会有 [Symbol.hasInstance]() 方法,所以大多数时候,Function.prototype[Symbol.hasInstance]() 方法指定当右侧是函数时 instanceof 的行为。该方法实现了 instanceof 操作符的默认行为(当 constructor 没有 [Symbol.hasInstance]() 方法时,算法相同)。

¥The instanceof operator calls the [Symbol.hasInstance]() method of the right-hand side whenever such a method exists. Because all functions inherit from Function.prototype by default, they would all have the [Symbol.hasInstance]() method, so most of the time, the Function.prototype[Symbol.hasInstance]() method specifies the behavior of instanceof when the right-hand side is a function. This method implements the default behavior of the instanceof operator (the same algorithm when constructor has no [Symbol.hasInstance]() method).

与大多数方法不同,Function.prototype[Symbol.hasInstance]() 属性不可配置且不可写。这是一项安全功能,可防止绑定函数的底层目标函数不可获取。请参阅 这个 StackOverflow 答案 的示例。

¥Unlike most methods, the Function.prototype[Symbol.hasInstance]() property is non-configurable and non-writable. This is a security feature to prevent the underlying target function of a bound function from being obtainable. See this StackOverflow answer for an example.

示例

¥Examples

恢复默认的 instanceof 行为

¥Reverting to default instanceof behavior

你很少需要直接调用此方法。相反,该方法由 instanceof 运算符调用。你应该期望这两个结果通常是相等的。

¥You would rarely need to call this method directly. Instead, this method is called by the instanceof operator. You should expect the two results to usually be equivalent.

js
class Foo {}
const foo = new Foo();
console.log(foo instanceof Foo === Foo[Symbol.hasInstance](foo)); // true

如果你想调用默认的 instanceof 行为,但你不知道构造函数是否具有重写的 [Symbol.hasInstance]() 方法,你可能需要使用此方法。

¥You may want to use this method if you want to invoke the default instanceof behavior, but you don't know if a constructor has a overridden [Symbol.hasInstance]() method.

js
class Foo {
  static [Symbol.hasInstance](value) {
    // A custom implementation
    return false;
  }
}

const foo = new Foo();
console.log(foo instanceof Foo); // false
console.log(Function.prototype[Symbol.hasInstance].call(Foo, foo)); // true

规范

Specification
ECMAScript Language Specification
# sec-function.prototype-@@hasinstance

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看