JS: Function.prototype[Symbol.hasInstance]
New in JS2015.
f[Symbol.hasInstance](obj)
-
Return
true
if the value off.prototype
is in the Prototype Chain of obj. Else,false
.〔see Symbol Tutorial〕
The following are equivalent:
f[Symbol.hasInstance](obj)
obj instanceof f
f.prototype.isPrototypeOf(obj)
〔see instanceof Operator〕
// Example Symbol.hasInstance on a buildin function const xx = [3, 4]; console.log(Array[Symbol.hasInstance](xx)); console.log(xx instanceof Array); console.log(Array.prototype.isPrototypeOf(xx));
// Example Symbol.hasInstance on a user-defined function function F() {} let xx = new F(); console.log(F[Symbol.hasInstance](xx)); console.log(xx instanceof F); console.log(F.prototype.isPrototypeOf(xx));