Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:56:49+00:00 2026-06-01T02:56:49+00:00

I encountered a problem after switching from server-side JavaScript to CoffeeScript in a Node.js

  • 0

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 #<TestObject> 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:56:51+00:00Added an answer on June 1, 2026 at 2:56 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've encountered a problem with JSP pages server-side caching. Suppose I have an internal
I encountered a problem dealing with UTF-8, XML and Perl. The following is the
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
My application has encountered a problem and needs to close. Of course Microsoft is
I have encountered a problem in my application. I have two forms, one that
I'm working on Ms Dynamics Crm 2011 development and I encountered following problem. My
I`ve encountered the problem after updating some gems, so basically all older gems are
I've encountered a problem after I set set my text in Android. Well the
I have encountered this problem: Depending on numbers from I want to create a
I wondered if anybody had encountered this problem. I'm writing an application for Android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.