RequireJS – is jQuery case sensitive?

Yes, jQuery defines itself as ‘jquery’, all lowercase. That’s normal. If you open the source to jQuery you’ll find: // Register as a named AMD module, since jQuery can be concatenated with other // files that may use define, but not via a proper concatenation script that // understands anonymous AMD modules. A named AMD … Read more

Using RequireJS, how do I pass in global objects or singletons around?

You would make that a module-level variable. For example, // In foo.js define(function () { var theFoo = {}; return { getTheFoo: function () { return theFoo; } }; }); // In bar.js define([“./foo”], function (foo) { var theFoo = foo.getTheFoo(); // save in convenience variable return { setBarOnFoo: function () { theFoo.bar = “hello”; … Read more

What is ‘define’ used for in JavaScript (aside from the obvious)?

I can’t say for sure without seeing the entire script, but it’s likely to be the define function from RequireJS, in particular the “define with dependencies” form of that function. It is used to define a “module”: A module is different from a traditional script file in that it defines a well-scoped object that avoids … Read more

Difference between RequireJS and CommonJS

Consider the first snippet, which is in the CommonJS style: var $ = require(‘jquery’); var _ = require(‘underscore’); var BackBone = require(‘backbone’); These calls are synchronous calls: when require returns, it returns the module you requested. CommonJS require calls are synchronous. There is a proposal for supporting asynchronous forms of require but as far as … Read more

Requirejs why and when to use shim config

A primary use of shim is with libraries that don’t support AMD, but you need to manage their dependencies. For example, in the Backbone and Underscore example above: you know that Backbone requires Underscore, so suppose you wrote your code like this: require([‘underscore’, ‘backbone’] , function( Underscore, Backbone ) { // do something with Backbone … Read more