RequireJS better organizing dependencies

I love RequireJS.  Javascript, with all it's flexibility and power, tends to lack a bit in organization and dependency management. ECMAScript 6 promises to fix with classes and modules, but until then we have RequireJS.  I'm not going to go over in detail how RequireJS works, but in short, it allows you to create modules which you can then reference between each other.  In some other languages this is akin to importing a class.  These modules and their dependencies are created with a define method which takes as parameters an array of dependencies, and a method defining the module contents, so it looks like this:

define([ ‘book’ ], function(book) {
//content returning module
});

Here you can see we are passing in the array the path to the module we want to include, then as a parameter in the method we define a variable to reference that module. This works fine in simple cases, however when we have long lists of dependencies, managing the order of the array and variables passed into the method can get confusing. Fortunately RequireJS provides another way of including dependencies:

define([], function(require) {
var book = require('book');
//content returning module
});

Using the require method we can include this module as a dependency, along with others in a clearly defined way. This breaks out of the array-matching-parameter silliness that can occur when you have a long list of dependencies. It also makes it easier to move them around between modules since they're one line each. Read more about RequireJS here.

Leave a Reply

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