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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:08:46+00:00 2026-05-23T06:08:46+00:00

I’d like to use namespaces as one would in javascript by using the keyword

  • 0

I’d like to use namespaces as one would in javascript by using the keyword “with”, but CoffeeScript reports this as a reserved keyword and refuses to compile is there any way I could use namespaces in cs?

In particular, I want to include a CoffeeScript file dynamically (trusted source), like loading models for a database schema, but I want the included script to have access to a local namespace.


Edit:

Here’s what I want to do. I am setting up a web framework that maps the directory tree to an application based on express and mongoose. For example, there’s a sub-directory ‘models’ that contains a file ‘user.coffee’ with code like this inside:

name:
    type: String
    unique: on
profiles: [ Profile ]

Whereby Profile is a class that sits in a local object named model. When the user model is being loaded, I wanted it to access the model classes that sit in my local model store.

My work-around for now was to write model.Profile into the file ‘user.coffee’. Hope it is clear what I mean.


2nd Edit

Here’s how I did it without using with:

user.coffee

name:
    type: String
    unique: on
profiles: [ @profile ]

profile.coffee

content: String

And here’s how it’s dynamically loaded:

for fm in fs.readdirSync "#{base}/models"
    m = path.basename fm, '.coffee'
    schema[m] = (()->
        new Schema coffee.eval (
            fs.readFileSync "#{base}/models/#{fm}", 'utf8'
        ), bare: on
    ).call model
    mongoose.model m, schema[m]
    model[m] = mongoose.model m

Seems an okay solution to me.

  • 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-23T06:08:47+00:00Added an answer on May 23, 2026 at 6:08 am

    The Coco fork of CoffeeScript supports a with syntax; see https://github.com/satyr/coco/wiki/additions. However, that syntax simply sets the value of this to the target object in a block, rather than compiling to the problematic and deprecated with keyword.

    Let’s say that you wanted to emulate Coco’s with syntax in CoffeeScript. You’d do something like this:

    withObj = (obj, func) -> func.call obj
    

    Then you could write

    withObj = (obj, func) -> func.call obj
    withObj annoyingly.lengthy.obj.reference, ->
      @foo = 'bar'
      @bar = 'baz'
    

    Of course, in such simple cases, it’s better to use a utility function like jQuery or Underscore’s extend:

    _.extend annoyingly.lengthy.obj.reference, foo: 'bar', bar: 'baz'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.