user1027169
user1027169

Reputation: 2887

module names in requirejs

When I load a module using requirejs, do I use the file name or the variable name defined in the module.

For example in the module definition mymodule.js

define( function () {

    var module = 'Hello World!';

    return module;

});

And then in the consuming module foo.js

Do I call it with:

require( [mymodule], function (module) {
    console.log(module);
});

Or

require( [mymodule], function (mymodule) {
    console.log(mymodule);
});

Upvotes: 0

Views: 784

Answers (1)

Simon Smith
Simon Smith

Reputation: 8044

Technically you can call it whatever you like, but one limitation of just using module is what do you do when you have two modules? module2? It's better (and more common) to name the parameter to match the module name:

require(['ajaxloader', 'modules/carousel', 'jquery'], function(ajaxloader, carousel, $){

});

In the above example you can see that even if the module has a path it makes sense to just use the module name. And there are always exceptions, in this case using $ instead of jquery.

Upvotes: 2

Related Questions