JS: Function.prototype[Symbol.hasInstance]

By Xah Lee. Date: .

New in ES2015.

f[Symbol.hasInstance] ( obj )

Returns 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: Function Parameters
  3. JS: var/function Order
  4. JS: var Name Scope
  5. JS: Function Declaration vs Function Expression
  6. JS: Closure
  7. JS: Functional Programing
  8. JS: Function Call, Apply, Bind
  9. JS: Function Argument Default Value
  10. JS: Function Rest Parameters
  11. JS: Arrow Function

  1. JS: Function Object
  2. JS: Function.prototype
Like what you read? Buy JavaScript in Depth