wprl
wprl

Reputation: 25427

instanceof evaluates to true in Node 4 but not Node 6

When I execute this in Node 4, the last statement evaluates to true, however in Node 6 it evaluates to false. Why?

F = () => {};
F.prototype = {};
Object.create(F.prototype) instanceof F;

Upvotes: 4

Views: 135

Answers (1)

Tamas Hegedus
Tamas Hegedus

Reputation: 29936

This is most probably a bug in Node 6.x. Consider the following:

const Foo = () => {};
Foo.prototype = {};
const foo = Object.create(Foo.prototype);
// false in Node 6, true in Chrome
console.log(foo instanceof Foo);
// true in Node 6, true in Chrome
console.log(Foo[Symbol.hasInstance](foo));

The first two logs should return the same value, because the instanceof operator is defined to call and return the @@hasInstance method of Foo, if present (§12.9.4). What is more interesting, node throws TypeError in the following case, while false is expected, as Foo is not callable (§7.3.19):

const Foo = {
  "prototype": {},
  [Symbol.hasInstance]: Function.prototype[Symbol.hasInstance]
};
const foo = Object.create(Foo.prototype);
// throws in Node 6, false in Chrome
console.log(foo instanceof Foo);
// false in Node 6, false in Chrome
console.log(Foo[Symbol.hasInstance](foo));

PS

Node v6.2.2 (64-bits) was used for the tests on a Windows system.

Upvotes: 2

Related Questions