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).
First class dependency objects via Require.JS
In this example,
jquery.js are fully loaded first, then passed as objects named
$, 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.
it should be noted that in your require.js example, without wrapping jquery in a define that returns jQuery, it does pollute the window object, with window.jQuery.
Tue, 02/21/2012 - 20:15