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 7513199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:06:14+00:00 2026-05-30T00:06:14+00:00

When I create a new coffeescript file, I cannot access the code in the

  • 0

When I create a new coffeescript file, I cannot access the code in the compiled code from another file because it gets wrapped in some function scope. For example:

CoffeeScript:

class ChatService
  constructor: (@io) ->

Generated Javascript:

(function() {
  var ChatService;    
  ChatService = (function() {    
    function ChatService(io) {
      this.io = io;
    }    
    return ChatService;    
  })();    
}).call(this);

When trying to call ChatService in another file, it’s not defined. How do I handle multiple files with coffeescript?

  • 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-30T00:06:16+00:00Added an answer on May 30, 2026 at 12:06 am

    Depending on whether this is client- or server-side code, there are two slightly different approaches.

    Client-side: Here we attach things that should be available across files to the global namespace (window) as follows:

    class window.ChatService
      constructor: (@io) ->
    

    Then, in another file both ChatService and window.ChatService will allow access to the class.


    Server-side: Here we must use exports and require. In the ChatService.coffee file, you would have the following:

    class exports.ChatService
      constructor: (@io) ->
    

    Then, to get at it from another file you can use:

    ChatService = require('ChatService.coffee').ChatService
    

    Note: If there are multiple classes that you are getting from ChatService.coffee, this is one place where CoffeeScript’s dict unpacking really shines, such as:

    {ChatService, OtherService} = require('ChatService.coffee')
    

    Both: Basically, we choose whether to run server-side or client-side code based on which environment we’re in. A common way to do it:

    class ChatService
      constructor: (@io) ->
    
    if typeof module != "undefined" && module.exports
      #On a server
      exports.ChatService = ChatService
    else
      #On a client
      window.ChatService = ChatService
    

    To get it:

    if typeof module != "undefined" && module.exports
      #On a server
      ChatService = require("ChatService.coffee").ChatService
    else
      #On a client
      ChatService = window.ChatService
    

    The else clause of the second block can be skipped, since ChatService already refers to the reference attached to window.

    If you’re going to define a lot of classes in this file, it may be easier to define them like:

    self = {}
    
    class self.ChatService
    

    And then attach them like module.exports = self on the server and _.extend(window, self) on the client (replace _.extend with another extend function as appropriate).

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

Sidebar

Related Questions

I create new process from a java code using ProcessBuilder ProcessBuilder builder = new
I want to create new class MapActivity from Eclipse. I go File-> New ->
How to create new PCL file similar to existing MS doc. I have MS
I tryed to create new components from one base Windows Form, also I found
to create new a new databse file start sql server management studio express on
I'm converting some javascript to coffeescript, and I'm having trouble accessing a function I've
how we can create new xml file by C# in the following situation: its
I need to create new warrior, assign name, and get his description with function
I create new ASP.NET web application that use SMTP to send message. The problem
I create new desktop with CreateDesktop and want to get it's DC & RC.

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.