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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:48:02+00:00 2026-06-15T02:48:02+00:00

How can I create namespace for my classes written in CoffeeScript? For example, I

  • 0

How can I create namespace for my classes written in CoffeeScript?

For example, I have tree classes Aa, Bb and Cc. I want them insert into globaly assesible namespace – MyClasses, allow comunicate cross them and using them in jasmine-node.

class MyClasses.Aa
 @someProp: true

class MyClasses.Bb
 @someProp2: false

class MyClasses.Cc
 @doSomeStuff: -> MyClasses.Aa.someProp = false

I know, I can inject them into one file and compile, but I want to have one class = one file.

How can I do that please?
Thank you!

EDIT: I tried this way, but I think it is not good, but it works in browser and jasmine-node

root = exports ? this
root.MyClasses = root.MyClasses ? {}

root.MyClasses.Aa = 

  class Aa
  • 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-15T02:48:04+00:00Added an answer on June 15, 2026 at 2:48 am

    Use RequireJS.

    In one file called “my-classes.coffee”, define the namespace.

    define [], ->
      # You need this if you want it to work in multiple environments.
      # Otherwise just use `window` to work in the browser.
      root = exports ? this
    
      root.MyClasses = {}
    

    You can define your class in another file called “aa.coffee”.

    define ['my-classes'], (MyClasses) ->
    
      class MyClasses.Aa
        @someProp: true
    

    Another file:

    define ['my-classes'], (MyClasses) ->
    
      class MyClasses.Bb
        @someProp2: false
    

    Now when you require, it should export MyClasses which includes MyClasses.Aa.

    require ['my-classes', 'aa'], (MyClasses, _) ->
      console.log MyClasses.Aa.someProp
    

    One issue with doing it this way is that you can’t depend on just “my-classes” in the require statement. If you did that, MyClasses.Aa would be undefined. But you can’t depend on just “aa” either, because “aa” doesn’t export anything except by adding to MyClasses. In the above code snippet, MyClasses.Bb is undefined because I haven’t explicitly depended on it. This is why many people either use one giant file or duplicate the boilerplate of re-exporting the namespace.

    If anyone knows how to fix this, please let me know.

    I, personally, find RequireJS to be complicated to use, and there are many different ways to set it up. One way I’ve used it with jasmine is by using a cake task to precompile my CoffeeScript down to JavaScript, and then have spec files like this.

    requirejs = require('requirejs')
    # Set the baseURL to your compiled JS dir.
    requirejs.config { baseUrl: __dirname + '/../lib' }
    
    requirejs ['my-classes', 'aa'], (MyClasses, _) ->
    
      describe "someProp", ->
        it "should be true", ->
          expect(MyClasses.Aa.someProp).toEqual true
    

    This may not be the best way, but I was able to use it to run modules in the browser, on a Node server, and in jasmine-node tests. I’ve also seen some people use custom runners to avoid the boilerplate in their spec files.

    If you’d rather not use RequireJS, you may find this question helpful. It works by using the namespace function defined on the CoffeeScript FAQs.

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

Sidebar

Related Questions

I have chicken<->egg problem. I want to create interface for a container classes, so
I want to dynamically create classes at runtime in python. For example, I want
In my namespace I can have public classes which are accessible to all other
How can i create an attribute with a namespace? To get the following output?
i have written a library with some classes that make use of qt object
Hi everyone: I am trying to create a namespace so I can use a
I have ClassLibrary project in C# and all my 'private classes' (under different namespace)
I have classes as follows namespace Coverage { public class ClassInfo { public string
I have 3 different classes Tree , Fruit , and Basket . Each of
I have a question regarding namespace names and classes: If I have a class

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.