Symbol.hasInstance
well-known symbol is used to determine if a constructor object recognizes an object as its instance. The
instanceof
operator's behavior can be customized by this symbol.
特性属性在
Symbol.hasInstance
|
|
|---|---|
| 可写 | no |
| 可枚举 | no |
| 可配置 | no |
You could implement your custom
instanceof
behavior like this, for example:
class MyArray {
static [Symbol.hasInstance](instance) {
return Array.isArray(instance)
}
}
console.log([] instanceof MyArray); // true
function MyArray() { }
Object.defineProperty(MyArray, Symbol.hasInstance, {
value: function(instance) { return Array.isArray(instance); }
});
console.log([] instanceof MyArray); // true
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Symbol.hasInstance' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
hasInstance
|
Chrome 50 | Edge 15 | Firefox 50 | IE No | Opera 37 | Safari 10 | WebView Android 50 | Chrome Android 50 | Firefox Android 50 | Opera Android 37 | Safari iOS 10 | Samsung Internet Android 5.0 |
nodejs
6.5.0
|
完整支持
不支持
用户必须明确启用此特征。
Symbol
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()