The code in question is this:
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
root.Backbone = factory(root, exports, _, $);
});
Note now factory
is called with exports
as the 2nd parameter. The factory function is going to export Backbone's methods by setting fields on this object. This is why when you require Backbone, you get a useful value and not undefined
or garbage.
This reason this works is that RequireJS supports defining a module by returning a value from the factory function you give to define
, so you can do this:
define(function () {
return {
foo: function () { ... }
};
});
But it supports also other means to do this, like for instance:
define(['exports'], function (exports) {
exports.foo = function () { ... };
});
In the code above the module named exports
in the list of dependencies is a special (and reserved) module name which means "give me an object on which I can set fields to export values".
Backbone uses this 2nd way of exporting a value.