1

I am working on a Node.js server and I am using coffee script to develop.

How does this work on coffee script?

EventEmitter = require('events').EventEmitter

util.inherits(Connector, EventEmitter)

Is it?

EventEmitter = require('events').EventEmitter

class @Connector extends EventEmitter

I am basically trying to add emit to Connector. Something like:

this.emit('online')
majidarif
  • 18,694
  • 16
  • 88
  • 133

1 Answers1

2

Yes, extends does a similar thing as util.inherits.

Implementation of util.inherits:

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

Compilation of extends:

var __hasProp = {}.hasOwnProperty,
__extends = function(child, parent) {
    for (var key in parent) {
        if (__hasProp.call(parent, key))
            child[key] = parent[key];
    }
    function ctor() {
        this.constructor = child;
    }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor();
    child.__super__ = parent.prototype;
    return child;
};

function Connector() {
    return Connector.__super__.constructor.apply(this, arguments);
}
__extends(Connector, EventEmitter);

The differences are:

  • The exact name of the super property on the child constructor
  • util.inherits uses Object.create while extends does use an ES3-compatible version
  • util.inhirits makes the constructor property of the child constructor non-enumerable
  • extend copies "static" properties of the parent constructor onto the child constructor
  • The extends keyword automatically calls the super constructor if no constructor is given for the class
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375