JS: Function .prototype [Symbol .hasInstance]

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2015)

f[Symbol.hasInstance](obj)
Return true if the value of f.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.assert(
 Array[Symbol.hasInstance](xx),
);
console.assert(
 xx instanceof Array,
);
console.assert(
 Array.prototype.isPrototypeOf(xx),
);
// Example Symbol.hasInstance on a user-defined function

function F() {}
const xx = new F();

console.assert(
 F[Symbol.hasInstance](xx),
);
console.assert(
 xx instanceof F,
);
console.assert(
 F.prototype.isPrototypeOf(xx),
);