Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Using util.inherits() "breaks" prototyping when using CoffeeScript

Using util.inherits() "breaks" prototyping when using CoffeeScript

Problem

I encountered a problem after switching from server-side JavaScript to Coffeescript in a Node.js application. Let's consider the following code:

# require our tools
util = require "util"
events = require "events"

# define an object class and a method using CoffeeScript syntax
class TestObject
  method: () -> #1
    console.log "Test"

# set up inheritance using Node.js util module
util.inherits TestObject, events.EventEmitter #2

# make an instance of object
instance = new TestObject()

# and boom! it crashes ;(
instance.method()

The above code will crash because of the error: TypeError: Object # has no method 'method'

The error is caused by setting up the inheritance at #2. The above code compiles to following JavaScript (deleted some newlines for the sake of readability):

(function() {
  var TestObject, events, instance, util;

  util = require("util");
  events = require("events");

  TestObject = (function() {
    function TestObject() {}

    TestObject.prototype.method = function() {
      return console.log("Test");
    };

    return TestObject;
  })();

  util.inherits(TestObject, events.EventEmitter);

  instance = new TestObject();
  instance.method();

}).call(this);

You can see that the util.inherits() method is called after adding the method to prototype. So, the method would be lost after switching prototypes.

Is there an elegant way to set up the inheritance without breaking the awesome CoffeeScript class notation?

Problem courtesy of: Przemek

Solution

class TestObject extends events.EventEmitter
  method: () -> #1
    console.log "Test"

Is there an elegant way to set up the inheritance without breaking the awesome CoffeeScript class notation?

You can just not use coffeescript and use objects

var extend = /* implement extend [snip] */

var TestObject = extend({}, events.EventEmitter.prototype, {
    method: function () {
        console.log("test")
    }
})

As an aside, unless you really like the CoffeeScript syntax you should stick to JavaScript. Especially for libraries, people who write open source libraries in CoffeeScript deserve to be shot.

Solution courtesy of: Raynos

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Using util.inherits() "breaks" prototyping when using CoffeeScript

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×