Next Generation JavaScript with AMD and RequireJS: New on Premium
Tutorial Details
- Topics Covered: RequireJS
- Format: Written and 1hr Video
I don’t know about you, but I’m always looking for better ways to organize my JavaScript. Recently (only a week or so ago), I’ve discovered what seems to be the best pattern yet. In this tutorial, I’ll introduce you to AMD: Asynchronous Module Definition and RequireJS. Hang on tight; it’ll be a wild ride!
Become a Premium member to read this tutorial/screencast, as well as hundreds of other advanced tutorials and screencasts from the Tuts+ network.
The Problem
If you’ve been writing client-side JavaScript for a while, you might have noticed a <sarcasm> small </sarcasm> problem with JavaScript. The only—or at least, the standard—way of getting multiple JavaScript files onto a page is to include multiple script tags:
<script src="file1.js"></script> <script src="file2.js"></script> <script src="file3.js"></script>
There are a couple of issues with this method.
Firstly, it’s just plain inconvenient. All the JavaScript in all the scripts is loaded into the global “namespace.” Of course, this may be misleading to beginners, who could easily—and excusable—think that identically-named variables in different files won’t conflict. But more than that, it’s a pain for experienced JavaScript developers with perhaps dozens of JavaScript files. Some files will depend on others, which may depend on others, etc. And you’re on playground duty: you’ve got to make sure they all arrive in the right order, and that nobody clobbers anybody. This can be both tricky or time-consuming, depending on the project.
Secondly—and more importantly—when a browser is downloading and executing JavaScript files, that’s all it’s doing; it’s blocking the downloading of other content until that’s done. This is why you’ve heard it recommended to put your <script> tags at the end of your <body>, instead of in your <head> as used to be the idea: that way, all your other content (HTML, CSS, images, etc.) will be visible to the user before the JavaScript is downloaded and executed. By using one of the many script loaders available, you’ll be able optimize this process at much as possible. RequireJS is one of those loaders, and you’ll get all the benefits of using it with AMD. For more on this, read the first chapter (and of course all the rest!) of High Performance JavaScript by Nicolas C. Zakas.
But all these script loaders—and all the other Javascript libraries and frameworks—cause a third problem that may not seem like a problem, but really is. You can (and should) read Stoyan Stefanov’s book JavaScript Patterns and get all kinds of great ideas for good coding patterns, or use frameworks like Dojo or Mootools that support great methods of modularization, etc. These patterns are a dime a dozen. However, they’re all different. In her talk “Modern JavaScript” at JSConf 2011, Rebecca Murphey said that she would love a JavaScript world where you could pick and choose where you get your functionality: you could get your DOM manipulation from jQuery, your array utilities from YUI, your UI widgets from Dojo, etc. Trying this today would be a nightmare. But, guess what? AMD solves this. We’ll see how.
Join Net Premium to Continue Reading…
For those unfamiliar, the family of Tuts+ sites runs a premium membership service. For $19 per month, you gain access to exclusive premium tutorials, screencasts, and freebies from Nettuts+, Psdtuts+, Aetuts+, Audiotuts+, Vectortuts+, and CgTuts+ For the price of a pizza, you’ll learn from some of the best minds in the business. Become a Premium member to read this tutorial, as well as hundreds of other advanced tutorials and screencasts.
- Daniel
- http://aryweb.nl Arian
- Andrea
- http://www.umarjadoon.com Umar
- Andrea
- Dhruv Kumar
- David
- http://www.nczonline.net/ Nicholas C. Zakas
- http://requirejs.org James Burke
- http://mastrotek.com web design new york
- Barry
- lukins
- lukins
- lukins
- http://www.123webdesignbournemouth.co.uk Web Design Bournemouth
- alexandre
- http://www.alignmyviews.com Dheeraj
- http://www.kingkroell.co.za Thomas | Website Design & SEO, Durban
- http://www.imstillreallybored.com Josh
- http://www.famesbond.com/ aditya menon

