I’m writing a book. It’s called JavaScript Testing Recipes.

JS.Class 2.1.5 supports Node, Narwhal and more

While there’s much work going on towards what will probably be JS.Class 3.0, the 2.1.x series is benefiting from some of the goodness being added upstream. I’ve just pushed out a new release that gets the package manager and all the libraries to work under CommonJS, specifically targeting Node.js and Narwhal for now.

I’ve had to make one tiny API change to avoid conflicting with the CommonJS API, so require() is now JS.require() and works just like it did before. To get your packages to work under CommonJS platforms, you don’t need to mess around with the exports object, you just need to remember this one rule:

If you want JS.Packages to find your object, do not declare it with var.

I’ll elaborate on this in a future post, but for now just remember that JS.Packages can only work with globally accessible objects, and using the var keyword (even outside a function) under CommonJS only makes the variable visible in the current file. If you stick to this rule and don’t use the exports object, you’ll have code that JS.Packages can run in any environment.

So, to get started using JS.Class on Node, just do what you’ve always done:

JSCLASS_PATH = './path/to/js.class';
require(JSCLASS_PATH + '/loader');

JS.require('JS.SortedSet', function() {
    var set = new JS.SortedSet([3,8,5,9]);
    require('sys').puts(set.count());
});

Note that the require() function is now called JS.require() in order to avoid conflicts with the CommonJS API. I thought about renaming it since I don’t like the fact that putting in the JS namespace makes it look like it can only load parts of JS.Class, but I honestly couldn’t think of a better name for it. Just remember you can load any library you like with it.

As usual, you can download JS.Class from its website.

9 thoughts on “JS.Class 2.1.5 supports Node, Narwhal and more

  1. “using the var keyword (even outside a function) under CommonJS only makes the variable visible in the current file.”

    That’s completely implementation-specific and isn’t part of the spec.

  2. The “JS.require(‘JS.SortedSet’…” snippet you provided doesn’t work in Node, for whatever reason (“ReferenceError: JS is not defined”).

    Also, the Package module a little confusing to me in general. Node’s require is synchronous. But the JS.Class system is asynchronous. Does that mean I have to wrap my code in a JS.require block for each library I want to load?

  3. On Node, JS.require() is actually synchronous, but it still takes a callback. The point is to abstract how script loading works on various platforms, some of which are asynchronous so we need an async API to accommodate them.

    I think I should write a full article to respond to the rest of your question; it’s not supposed to be that obtrusive and typically only ends up in small bits of glue code the way I end up using it.

  4. Hi, I’m working with node.js and trying to figure out how to manage modular code with JS.class.
    Simple question : I have a file with a JS Class . Now, how do I import this class in another file/module ?

  5. “using the var keyword (even outside a function) under CommonJS only makes the variable visible in the current file.” That’s completely implementation-specific and isn’t part of the spec.

  6. Mario, could you be more specific about what doesn’t work? The example works fine for me and I never have problems using this approach in Node or in browsers.

    Nona, thanks for pointing that out. I was going off guess-work from making this work on Node and Narwhal, so good to know what I can rely on.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>