Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Created June 1, 2016 05:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericelliott/f15875950310c6b51be31286454cfbbd to your computer and use it in GitHub Desktop.
Save ericelliott/f15875950310c6b51be31286454cfbbd to your computer and use it in GitHub Desktop.
instanceof doesn't mean what you think it means...
// instanceof is a prototype identity check.
// NOT a type check.
// That means it lies across execution contexts,
// when prototypes are dynamically reassigned,
// and when you throw confusing cases like this
// at it:
function foo() {}
const bar = { a: 'a'};
foo.prototype = bar;
// Is bar an instance of foo? Nope!
console.log(bar instanceof foo); // false
// Ok... since bar is not an instance of foo,
// baz should definitely not be an instance of foo, right?
const baz = Object.create(bar);
// ...Wrong.
console.log(baz instanceof foo); // true. oops.
@jeffmcmahan
Copy link

instanceof is a prototype identity check.

Leads to things like:

((() => 0) instanceof Object) === true // lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment