Why is amd-style define definition anonymous? #3055

mmucklo opened this Issue Mar 10, 2014 · 6 comments

6 participants


For underscore.js, the define call includes the name of the library, however for backbone it does not.

This leads to some interesting error messages when things are rolled up into a single file which includes require.js at the top.

For example, underscore.js does this:

    define('underscore', [], function() {

Where as backbone.js does this:

    define(['underscore', 'jquery', 'exports'], function(_, $, exports) {

It would be nice if backbone.js did this instead:

    define('Backbone', ['underscore', 'jquery', 'exports'], function(_, $, exports) {

However I'm not sure of the implications of that with no-conflict mode...


I don't know the answer to that question. @jrburke?

@jashkenas jashkenas added the question label Mar 10, 2014

Anonymous modules are the preferred way to define modules -- it allows for more patterns of use by consumers. Once modules are combined together, the AMD-aware optimizer that combines the modules inserts the names.

This process is similar in a node+browserify approach and how it will be for ECMAScript 6 too -- the modules in source form do not name themselves, but once bundled will have the IDs that pertain to that project/bundling scenario.

Given the issues with loading scripts in browsers that all share the global scope, and the insufficient browser capabilities (across all browsers, modern browsers are better) it is not always possible for anonymous modules to work in some scenarios. This is mostly an issue for very basic, core scripts like jQuery and underscore. However, for higher level scripts, like Backbone, this is less of a concern. More details in this wiki page about upgrading libraries.


👍 anonymous. I've had to add functionality to my build tool to actually rename modules that name themselves because they didn't register anonymously.


"... it allows for more patterns of use by consumers."

@jrburke Can you expand on this statement in more detail?


For instance you can have two different backbone versions available under the same app (this sounds silly, but it's just a benefit example of using anonymous define)


@AjaxSolutions: consumers of the backbone library may not want to refer to it as "backbone". Examples, ordered by what I expect is most likely as first:

A developer may want to add a few a modifications to backbone itself, but just refer to "backbone" as the dependency in their app modules. So they could set up a "backbone" module that does a require("backbone-base"), which is the real backbone, do their modifications, then export that value as the value for 'backbone'. So their own module is sort of a proxy to the real backbone.

Or they prefer to have a their mvc library called 'mvc', and allow swapping out/in backbone for that 'mvc' ID.

Or for some horrible reason they need to load two different versions of backbone in the page. This would be an awful scenario, but has been known to happen (not necessarily with backbone, just in general with modules and versions of modules).

By keeping the module anonymous, it allows those uses.

@jashkenas jashkenas closed this Mar 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment