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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:21:38+00:00 2026-06-18T10:21:38+00:00

related: Classes within Coffeescript 'Namespace' OK so after reading that post I grabbed the

  • 0

related: Classes within Coffeescript 'Namespace'

OK so after reading that post I grabbed the namespace function and put it in its own file.

namespace.coffee

namespace = (target, name, block) ->
  [target, name, block] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length < 3
  top    = target
  target = target[item] or= {} for item in name.split '.'
  block target, top
  console.log "created namespace "+ name


root = exports ? window
root.namespace = namespace

and then in Repl:

>  namespace = require('./assets/js/namespace.js').namespace
[Function]                                              

If I toString() it it’s correct.

OK, so now I want to use it: ns.coffee (from Sandro’s answer)

namespace = require('./namespace.js').namespace

class MyFirstClass
  myFunc: () ->
    console.log 'works'

class MySecondClass
  constructor: (@options = {}) ->
  myFunc: () ->
    console.log 'works too'
    console.log @options

namespace "Project.Something", (exports) ->
  exports.MyFirstClass = MyFirstClass
  exports.MySecondClass = MySecondClass
  console.log 'done with exports'

Then I run it in Repl:

ns = require(‘./assets/js/ns.js’) # compiled ns.coffee
done with exports
created namespace Project.Something
{}

It doesn’t appear to be working:

> ns.MyFirstClass                                              
undefined                                                      
> ns.MySecondClass                                             
undefined                                                      
> ns.Project.Something.MySecondClass                           
TypeError: Cannot read property 'Something' of undefined      

Am I doing something wrong here?

  • 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-18T10:21:40+00:00Added an answer on June 18, 2026 at 10:21 am

    The exports variable is the module.exports for the file you reference it from. So when you call namespace, it changes exports that was returned from requireing namespace, not the one that will be returned from requireing ns.

    Notice how namespace takes 3 arguments, but the first line in it essentially makes target optional. If you pass in the target (you probably want exports, or maybe exports ? window if this needs to run in both server and client), then it should do what you want.

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

Sidebar

Related Questions

I have a number of classes that are all related conceptually, but some more-so
I am frequently having naming conflicts between a namespace and a class within that
I need to test some security related classes that depend on Spring Security. This
As I know The DataGridView control and its related classes are designed to be
imagine I have a bunch of C++ related classes (all extending the same base
I have a simple Fluent NHibernate model with two related classes: public class Applicant
I have three classes: PurchaseOrder, PurchaseOrderLine, Item PurchaseOrder children are PurchaseOrderLine which is related
Related: see here I've got this command: exec((wget -O http://domain/file.zip && mysql -u user
Related to an earlier question I asked, I've seen that there's both LoadComponent() and
It seems like whenever I try to use XML-related classes in my Dynamic Web

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.