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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:57:37+00:00 2026-06-01T16:57:37+00:00

I have a class written in coffeescript, e.g., class Example constructor: -> $.each [1,

  • 0

I have a class written in coffeescript, e.g.,

class Example
  constructor: ->
    $.each [1, 2, 3], (key, value) =>
      @test = value
    return @test
  render: ->
    alert @test

and I have this class as a separate file, Example.coffee

Now I want to be able to instantiate in my main javascript file like so:

d = new Example
d.render()

but the class is undefined even when it is included as a script on the page, like

<script src="Example.js></script>
<script src="main.js"></script>

How do you make the class publicly available to the main file?

  • 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-01T16:57:38+00:00Added an answer on June 1, 2026 at 4:57 pm

    You can declare your class to be globally accessible (at least for browsers) by declaring it to be in the window namespace:

    class window.Example
      constructor: ->
        $.each [1, 2, 3], (key, value) =>
          @test = value
        return @test
      render: ->
        alert @test
    

    That will put Example straight into window. You can also say class @Example in most cases.

    By default, CoffeeScript wraps each file in a (function() { ... })() wrapper to prevent namespace pollution. You can prevent this by supplying -b when compiling your CoffeeScript:

    -b, --bare
    Compile the JavaScript without the top-level function safety wrapper.

    but that might not be an option for you (or it might be an ugly one). The usual approach is to declare an application specific namespace somewhere before your classes are loaded:

    // Probably in a <script> in your top-level HTML...
    App = { };
    

    and then namespace your classes appropriately:

    class App.Example
        #...
    

    Then refer to everything through the App namespace.

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

Sidebar

Related Questions

This has been stumping me for a bit. I have a Class written in
I have written this class. This is not complete description of original class to
I have a class written in c# which is acting as a wrapper around
I have written a class in python that implements __str__(self) but when I use
I have written a class using std::tr1::regex, and I don't know how to link
I have written a class that will handle internal logging in my application. Now
I have a class library with some extension methods written in C# and an
I have a class that I've written a TypeConverter for. I want to keep
I have inherited a project that has class libraries written in VB.NET, some of
I am working on struts2. In my action class I have written some accessors

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.