Note that there are some explanatory texts on larger screens.

plurals
  1. POhow do I use coffeescript's namespace code in Node?
    text
    copied!<p>related: <a href="https://stackoverflow.com/questions/8730859/classes-within-coffeescript-namespace">Classes within Coffeescript &#39;Namespace&#39;</a></p> <p>OK so after reading that post I grabbed the namespace function and put it in its own file.</p> <p><code>namespace.coffee</code> </p> <pre><code>namespace = (target, name, block) -&gt; [target, name, block] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length &lt; 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 </code></pre> <p>and then in Repl: </p> <pre><code>&gt; namespace = require('./assets/js/namespace.js').namespace [Function] </code></pre> <p>If I toString() it it's correct. </p> <p>OK, so now I want to use it: <code>ns.coffee</code> (from Sandro's answer)</p> <pre><code>namespace = require('./namespace.js').namespace class MyFirstClass myFunc: () -&gt; console.log 'works' class MySecondClass constructor: (@options = {}) -&gt; myFunc: () -&gt; console.log 'works too' console.log @options namespace "Project.Something", (exports) -&gt; exports.MyFirstClass = MyFirstClass exports.MySecondClass = MySecondClass console.log 'done with exports' </code></pre> <p>Then I run it in Repl: </p> <blockquote> <p>ns = require('./assets/js/ns.js') # compiled ns.coffee<br> done with exports<br> created namespace Project.Something<br> {} </p> </blockquote> <p>It doesn't appear to be working: </p> <pre><code>&gt; ns.MyFirstClass undefined &gt; ns.MySecondClass undefined &gt; ns.Project.Something.MySecondClass TypeError: Cannot read property 'Something' of undefined </code></pre> <p>Am I doing something wrong here? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload