JS: Function.prototype[Symbol.hasInstance]

By Xah Lee. Date: .

New in ES2015.

f[Symbol.hasInstance] ( obj )

Return true if the value of f.prototype is in the prototype chain of obj. Else, false.

[see JS: “this” Binding]

[see JS: Prototype and Inheritance]

[see JS: Symbol Tutorial]

The following are equivalent:

Example of buildin function.

const ar = [3,4];

console.log ( Array[ Symbol.hasInstance ](ar) ); // true
console.log ( ar instanceof Array ); // true
console.log ( Array.prototype.isPrototypeOf ( ar ) ); // true

Example of user-defined function.

function F () { }

let ff = new F;

console.log ( F[ Symbol.hasInstance ](ff) ); // true
console.log ( ff instanceof F ); // true
console.log ( F.prototype.isPrototypeOf ( ff ) ); // true

[see JS: “instanceof” Operator]

[see JS: Symbol Tutorial]

Reference

ECMAScript 2015 §Fundamental Objects#sec-function.prototype-@@hasinstance

Function Topic

  1. JS: Define Function
  2. JS: Arrow Function
  3. JS: Function Parameters
  4. JS: var/function Order
  5. JS: var Name Scope
  6. JS: Function Declaration vs Function Expression
  7. JS: Closure
  8. JS: Functional Programing
  9. JS: Function Call, Apply, Bind
  10. JS: Function Argument Default Value
  11. JS: Function Rest Parameters
  12. JS: Function Argument Destructure

  1. JS: Function Object
  2. JS: Function.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth