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

  • SEARCH
  • Home
  • 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 6837855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:31:36+00:00 2026-05-26T23:31:36+00:00

I have the following CoffeeScript code example: class TestClass constructor: () -> @list =

  • 0

I have the following CoffeeScript code example:

class TestClass
    constructor: () ->
        @list = new Object()

    addToList: (key, value) ->
        @list[key] = value

    printList: () ->
        console.log("This is printed from printList:", @list)

    startHttp: () ->
        http = require("http")
        http.createServer(@printList).listen(8080)

test = new TestClass()
test.addToList("key", "value")
test.printList()
test.startHttp()

When I run the code, and make a HTTP request to 127.0.0.1:8080, I expect to get the following output:

This is printed from printList: { key: ‘value’ }
This is printed from printList: { key: ‘value’ }

But I get the following instead:

This is printed from printList: { key: ‘value’ }
This is printed from printList: undefined

Why is it the printList function can’t access the list variable when it is called from the HTTP server?

I am using Node.js v0.6.1 and CoffeeScript v1.1.3.

  • 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-05-26T23:31:37+00:00Added an answer on May 26, 2026 at 11:31 pm
    printList: () =>
        console.log("This is printed from printList:", @list)
    

    Use => to bind the value of this to the function so it “works” as you expect.

    Disclaimer: instances might break. Coffeescript is black magic for all I care.

    What you really want to do is invoke the method on the correct object

    that = this
    http.createServer(->
      that.printList()
    ).listen 8080
    

    Or in plain javascript.

    var that = this;
    http.createServer(function () {
      that.printList();
    }).listen(8080);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code inside a class. (It's coffeescript-- and it's for a
I have following code snippet in c#. var list = new List<string> { a,
I have following code in Java: import java.util.*; public class longest{ public static void
I have the following code for example $(a.foo).bind(function (e){ var t; if ( $(e.target).is(a)
I have some coffeescript like the following: class foo: @bar = 'bob loblaw' processRows:
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following classes. class A { public: void fun(); } class B: public
I have following div: <div id = zone-2fPromotion-2f class = promotion> How can I

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.