0

I recently received a code review from a colleague where he wanted my opinion on a different approach for defining JS modules and pulling in dependencies with requireJS. The typical approach is:

define('module-name', 
    ['dep1', 'dep2', 'dep3'], 
    function(dep1, dep2, dep3){
        //Do stuff...
});

The approach he submitted for review is:

define('module-name', 
    function(){
        var dep1 = require('dep1');
        var dep2 = require('dep2');
        var dep3 = require('dep3');
        //Do stuff...
});

His reasoning behind declaring each require verbosely was that it can be a pain when adding or removing dependencies to the array. You have to be sure the order of your parameters in the callback function is correct. His method guarantees that you can't accidentally remove a dependency and leave the parameter.

I know that the first approach is what's detailed in the requireJS documentation, but is there any sort of difference? My only argument against his approach is that it's not what's typically done, but I'd like to know if there are any technical reasons against it.

DBosley
  • 88
  • 7
  • Functionally as far as the code within the module is concerned there is no difference. As far as how requirejs handles resolving the dependencies in the first vs the second example, I do not know - I would have to look at the internals. – Brian Driscoll May 17 '16 at 18:08
  • @BrianDriscoll That's exactly what I told him, and why I figured asking SO was the right approach – DBosley May 17 '16 at 18:10
  • The one thing I do know is that each call to `require()` inside the module definition is synchronous and blocking. – Brian Driscoll May 17 '16 at 18:12

0 Answers0