javascript - with - learn coffeescript

coffeescript Class not accessible in main javascript (2)

Create a global variable

window.Example = Example

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,

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

d = new Example

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?

I know this is an old thread, but in case anyone else finds it useful, declare your class with "@" and it will be accessible to .js files outside of the .coffee file.

So, in

class Introverted
  honk: ->
    alert "This class is visible within the .coffee file but not outside"

class @Extroverted
  honk: ->
    alert "This class is visible inside and outside of the .coffee file"

That is compiled to example.js which can then be used in example.html:

<script src="example.js"></script>
var p = new Extroverted(); // works fine

var i = new Introverted(); // will fail with "Introverted is not defined"