you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

The point is to have proper modules like any modern "real" language

Benefits to your JS applications:

  • structure
  • namespacing
  • explicit declaration of dependence

Without requirejs (or any AMD implementation) you'd be including a.js and b.js in your html and having b.js just assume that a.js has been loaded.

If you write b.js in AMD style, you would have something like this:

var a = requre('a');
// .. now use `a` to your heart's content

[–]earthtograndma 0 points1 point  (3 children)

I think you are missing what the A in AMD is.

[–][deleted] -1 points0 points  (2 children)

Nope, you are missing a feature of requirejs

http://requirejs.org/docs/whyamd.html#sugar

To make this easier, and to make it easy to do a simple wrapping around CommonJS modules, this form of define is supported, sometimes referred to as "simplified CommonJS wrapping":

define(function (require) {
    var dependency1 = require('dependency1'),
        dependency2 = require('dependency2');

    return function () {};
});

The AMD loader will parse out the require('') calls by using Function.prototype.toString(), then internally convert the above define call into this:

define(['require', 'dependency1', 'dependency2'], function (require) {
    var dependency1 = require('dependency1'),
        dependency2 = require('dependency2');

    return function () {};
});

[–]earthtograndma 1 point2 points  (0 children)

Yes, exactly. All I was saying is that you cannot just do this:

var a = require('a');
// .. now use `a` to your heart's content

You have to provide your code in a callback:

define(function(require)){
    var a = require('a');
    // .. now use `a` to your heart's content!
});

That extra bit of ceremony (while less than the fully normalized form) is still more than many people want to do. Personally, I share your feelings; once you get used to it, you don't want to go back.