Currently the AMD syntax requires a large amount of repetition just to always get access to our required modules, this could be simplified by doing the following:
/**
* Defines a new module
*
* @param {String} name An optional name for the module || filename.
* @param {Array} dependencies Dependencies for the module, can be omitted.
* @param {Function|Object} module The Modules code.
**/
define([name,] [dependencies,] module);
I propose that the signature of the module argument be changed such that if module is a function, then the signature is:
function(dep1, dep2, dep3, dep4)
The changes here are that I've removed the require
and exports
members,
instead, you would attach methods to the this
variable. Example:
define('alpha', ['beta', 'gamma'], function(beta, gamma) {
this.verb = function() {
return beta.foo;
}
});
Essentially, if you're needing to call require() then you should really be defining that as a module dependency.
Likewise for object literal definitions:
define('alpha', ['beta', 'gamma'], {
verb: function() {
return this.dependencies.beta.foo;
}
});
The third code snippet you show will work in the current AMD loaders (RequireJS, bdLoad, and curl.js at least) since they're all CommonJS Modules 1.1 compliant and node.js compliant. Specifically,
this
=exports
in node.js. The last example you show is interesting. :)