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.
“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.
Oops. Ignore my previous comment. I misread the sentence completely. That’s of course true.
This is interesting. base2 and JS.Class are very similar. I still don’t like seeing “new Class” though. :)
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?
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.
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 ?
Could you provide a few simple clear cut examples of using a shared Js.Class in Node.js as well as on the Browser simultaneously?
Your example above does not work.
“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.
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.