Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I like the modular aspect of AMDs (Asynchronous Module Definition; see this Stack Overflow reply) and it's quite neat to structure JS.

But I typically compile all my JS files into one JS file. Therefore I don't really need the asynchronous part. So I thought I could quickly write such "infile" Module Loader.

var load=(function()
{
  var modules={};
  return function(moduleName,dependencies,module)
  {
    if(moduleName.constructor===Array)
    {
      module=dependencies;
      dependencies=moduleName;
      moduleName=undefined;
    }
    if(!((moduleName&&moduleName.constructor===String||moduleName===undefined)
         && dependencies&&dependencies.constructor===Array
         && module&&module.constructor===Function))
      throw "wrong usage";
    var ret = module.apply(null,
      dependencies.map(function(d){return modules[d]||(function(){throw "no module "+d})()})
    );
    if(moduleName)
    {
      if(!ret||ret.constructor!==Object) throw "module should be an object";
      modules[moduleName]=ret;
    }
  }
})();

load('utils',[],function(){return {veryUseful:function(){alert('hey')}}});
load(['utils'],function(utils){utils.veryUseful()});

load is used on one way to define a module and on an other way to call a function with the required modules passed as function arguments.

Demo

The goal here is to allow one to have two pieces of code written in the same file that are guaranteed to be independent.

I'm curious if you have any suggestions on any level. From the concept of such "infile" Module Loader to a hidden neat little JS feature.

share|improve this question
    
"see this StackOverflow reply" -> That's not a reply. That's an user profile. –  luiscubal Aug 10 '12 at 15:41
    
thanks, just edited the link. It's a reply now –  brillout.com Aug 10 '12 at 15:44
    
I quite like it. To anyone that has used require.js your module definitions will feel natural. –  Jivings Feb 3 at 21:37

1 Answer 1

I re-read your code several things, I can't say I would want to use it.

  • Requiring the parameters to be modules seems neat with 1 module or 2. For larger applications, that would become silly.

  • The most common use case with require is var xxx = load("Xxx") but you do not provide anything of the sort, in fact, load does not return anything

  • Having load being called in 2 different ways with 2 different ways of execution is wrong, I would suggest to simply use 2 different functions.

  • Consider rewriting the if block for "wrong usage" with falsey values and/or add some comment, it is pretty much unreadable now

At the very least I would modify the apply call to a call call, so that this has all the modules :

var ret = module.call(modules);

which makes for

load('utils',[],function(){return {veryUseful:function(){alert('hey')}}});
load(['utils'],function(){this.utils.veryUseful()});

and return ret in case the caller wants to work with the module directly.

share|improve this answer
    
Not sure I agree with using this to access the modules. It doesn't feel very 'modular'. –  Jivings Feb 3 at 21:35
    
Agreed, but if I had to choose between parameters or this.. –  konijn Feb 3 at 23:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.