First class dependency objects in ECMAScript Harmony

My favorite perk of Require.JS is how dependencies are sent as arguments to a factory function. Treating dependencies as first class objects makes much more sense to me than polluting the window object - something that happens too much with other dependency management solutions (I'm looking at you, Steal.JS).

I was curious how I might get this functionality with the next generation of JavaScript - namely, ECMAScript Harmony. No browsers implement Harmony's module system just yet, so pardon the pseudocode.

First class dependency objects via Require.JS

function(helloworld, $){

In this example, helloworld.js and jquery.js are fully loaded first, then passed as objects named helloworld and $, respectively. Once in the factory function, I have free reign over the dependencies' methods, properties, and prototypes.

First class dependency objects via ECMAScript Harmony

module helloworld from '../src/helloworld.js';
module $ from '../lib/jquery.js';

This is the same result as Require.JS - the dependencies are loaded asynchronously (Harmony loads modules at compile time), and the dependencies are nicely encapsulated in their respective first class objects.

As it stands, I find the Require.JS syntax to be more readable. The list of dependencies and their mapping to objects is crystal clear. Since I haven't been able to test ECMAScript Harmony modules in a browser yet, the jury is still out on the pleasures of the new module syntax.

Filed under 

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.

About the Author

Tony Rasmussen, Vice President of Engineering

Tony believes that customers' technology problems can be solved with deep respect, sound data, strong process, and adventurous teams. He uses data-driven methods to improve all stages of the development lifecycle – from design, to beta, to final deployment. With numerous ties to the open-source community, Tony also works to solve client problems faster and more effectively with well-tested open-source solutions.

Several dozen products have shipped under his guidance over the past 14 years: collaborative internal sites at Nike, social networking integrations with Adidas, server-monitoring websites at Hewlett Packard, open source contributions to Drupal, entertainment sites such as The Emmys, community sites such as FearNET, and HTML5 apps for tablets and Smart TVs.