util.inherits - alternative or workaround

I am a n00b in node, and find util.inherits() very useful, except for the fact that it seems to replace the entire prototype of the original object. For instance:

var myClass = function(name){
    this._name = name;  
};

myClass.prototype = {
    (...)
};

util.inherits(myClass, require('events').EventEmitter);

seems to erase my original prototype.

That brings me two inconveniences:
1 - I have to declare add properties to my prototype after calling inherits,

var myClass = function(name){
    this._name = name;  
};

util.inherits(myClass, require('events').EventEmitter);

myClass.prototype.prop1 = function(){...};
myClass.prototype.prop2 = function(){...};

and, most important, i think i cannot inherit from two or more different classes.

Can anyone explain to me why this makes sense and what would be a good way to work around this?

Thanks

It does not make sense that you have to declare your prototype after util.inherits(). My guess is util.inherits originated as an internal-use-only method, tailored only for the limited internal use-cases it was initially intended for, which at some point got published for general usage. The util module is written in pure JS, so it is very easy to implement your own version of util.inherit that preserves your prototype. Here's the original util.inherit source:

exports.inherits = function(ctor, superCtor) {
  ctor.super_ = superCtor;
  ctor.prototype = Object.create(superCtor.prototype, {
    constructor: {
      value: ctor,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });
};

As for the multiple inheritance, that's going to be a much more difficult problem to tackle, as Javascript's prototype inheritance is not really suited for multiple inheritance at all. Each instance has only a single internal [[Prototype]] property, which is used to look up members that are not found in the actual instance. You could merge the prototypes of two separate "parent classes" into a single prototype, but you will then lose the inheritance to their parents, and you will lose the ability to change the parent prototype and have all children see the change.

You should first inherit, after the function definition, and then implement your object. Also don't forget to call the superclass constructor in your class's constructor.

Function A(y){
  this.y = x;
}

Function B(x,y){
  this.x = x;
  A.call(this,y);
}

util.inherits(B,A);

B.prototype.mythodA = function() {
    //do something
}

I very much wanted the same thing and was unhappy with extend, so I created this extension to the already useful util.inherits method:

var util = require('util');

module.exports = {
    inherits : function(sub, sup, proto) {
        util.inherits(sub, sup);
        if (typeof proto !== 'undefined') {
            Object.keys(proto).forEach(function(key) {
                sub.prototype[key] = proto[key];
            });
        }
    }
};

I put this in my project's ./util/index.js and then do this to use it:

var EventEmitter = require('events').EventEmitter;
var util = require('./util');

function Foo() {
    EventEmitter.call(this);
}

util.inherits(Foo, EventEmitter, {
    bar : function(){
        console.log(this instanceof EventEmitter); // true
    }
});

Maybe I'll publish this if I find it's robust and useful more and more. I just barely implemented it myself, so I'm giving it a test run.

Let me know what you think!

NOTE: This does override methods on either of the classes with the ones defined in the proto hash at the end. Just be aware of that.

If you use CoffeeScript + node.js, use extends when declaring class:

EventEmitter = require('events').EventEmitter

class MyClass extends EventEmitter
    constructor: ->
        EventEmitter.call this

    myFunction: ->
        @emit 'error', 'Some error message.'

Then it works as expected. Code:

myClass = new MyClass()

myClass.on 'error', (e) ->
    console.log e

myClass.myFunction()

Outputs:

Some error message.