Open repository brings modularity to JavaScript

Share this article

It’s early days, but a group of Perl hackers have banded together to form JSAN, The JavaScript Archive Network. Like the venerable Perl equivalent, CPAN, and other sites that borrowed inspiration (e.g. PEAR for PHP), JSAN aims to be an open repository for JavaScript scripts that are designed in a modular fashion so as to have manageable dependencies that encourage code reuse.

In practical terms, what this means is you can choose a particular module that provides functionality you’d like on your site, and the JSAN system will automatically download and install not only the files for that module, but also the files of any modules upon which that module depends. With the files installed in your site’s directory structure, you can then go ahead and use the functionality they provide in your own scripts.

As JSAN was conceived by Perl hackers, the automated system for downloading and installing JSAN modules is naturally written in Perl. If you’re a developer who works on a Windows box, I sympathise with that groan you just let out. If it’s any consolation, there are rumblings of an “alternative JSAN client” in the works, so fingers crossed. In the meantime, you need to install Perl and get familiar with your operating system’s command prompt before you can install and use JSAN.

Documentation is early-to-nonexistent at this stage, however anyone with some familiarity with CPAN or PEAR and a strong grasp of JavaScript should be able to figure it out based on the nascent installation guide that does exist. Documentation for using individual modules once installed is provided within the script files themselves in a standard format used on CPAN.

Aside from dependency tracking and auto-installation of scripts, JSAN places very few constraints on how scripts should be written. In fact, standalone modules can be obtained easily by browsing to the appropriate script file on the site and downloading it directly–no Perl client required. The initial library of modules, however, clearly favours unobtrustive scripting with JavaScript classes used as namespaces, as presented in DHTML Utopia.

If you do use the official client, you’ll benefit from a standardized directory structure, which will let you use JSAN’s built-in import mechanism for scripts. A trick I first saw in the jsolait library for XML-RPC, the system allows a script to load another script that it requires on-the-fly, instead of requiring the Web developer to load that script with a separate

All technical rambling aside, what this means is that if your script requires the List.Utils CPAN module, you can load it automatically if necessary using this code in your script:

try {
if (!List.Utils) JSAN.use('List.Utils');
} catch (e) {
throw 'This script requires the List.Utils module from JSAN.';

The nitty gritties are documented in the burgeoning JSAN wiki.

One tasty nugget on the wiki is a list of browser compatibility issues to consider when writing scripts for JSAN. Some deliciously obscure gotchas to be found there, like the fact that “true” and “false” (the strings) both evaluate to true in JavaScript.

Also worthwhile following is Planet JavaScript, an aggregation of a number of top JavaScript blogs along with JSAN module releases.

Kevin YankKevin Yank
View Author

Kevin Yank is an accomplished web developer, speaker, trainer and author of Build Your Own Database Driven Website Using PHP & MySQL and Co-Author of Simply JavaScript and Everything You Know About CSS is Wrong! Kevin loves to share his wealth of knowledge and it didn't stop at books, he's also the course instructor to 3 online courses in web development. Currently Kevin is the Director of Front End Engineering at Culture Amp.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form