Skip to content

Instantly share code, notes, and snippets.

@cyjake
Last active January 3, 2016 00:19
Show Gist options
  • Save cyjake/8381942 to your computer and use it in GitHub Desktop.
Save cyjake/8381942 to your computer and use it in GitHub Desktop.
Creating sub classes with prototype assigning directly will confuse instanceof operator.
var util = require('util')
function Class() {}
function SubClass() {}
function WrongSubClass() {}
util.inherits(SubClass, Class)
util.inherits(WrongSubClass, Class)
WrongSubClass.prototype = Class.prototype
var foo = new SubClass()
console.log(foo instanceof WrongSubClass) // ==> logs true, should be false obviously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment