According to the node.js docs:
util.inherits(constructor, superConstructor)
Inherit the prototype methods from one constructor into another. The prototype of constructor will be set to a new object created from superConstructor.
You are passing in an object where util.inherits
was designed to expect a constructor. util.inherits
operates by redefining the constructor's prototype
property, which has prototype chain implications for instances constructed by that constructor. Instead of a constructor function, you have passed in an object, which has its prototype
property altered. This has no implications for anyting: you've just created a property on the object called "prototype
", which is exactly as special as if you'd made a property on the object called foobar
.
There is a school of thought that eschews use of new
(in favor of factory functions that call Object.create
-- see discussion on Is JavaScript's "new" keyword considered harmful?), but evidently the API designers who implemented util.inherits
do not share that view.
Your attempted Object.create(EventEmitter)
is indeed not correct, however, because Object.create
expects a prototype object, not a constructor function. Instead, you'd do Object.create(EventEmitter.prototype)
. This is the same as calling new EventEmitter()
, except that the Object.create
variant does not call the EventEmitter
constructor function.