I like the modular aspect of AMDs (Asynchronous Module Definition; see this Stack Overflow reply 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.
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.
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.
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.
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.
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.
I like the modular aspect of AMDs (Asynchronous Module Definition,Definition; see this StackOverflowStack Overflow reply) and it's quite neat to structure JS. But
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.
Here it is:
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()});
So 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.
A demo at http://jsfiddle.net/suKXm/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 guys have any suggestions on any level. From the concept of such "infile" Module Loader to a hidden neat little JS feature.
I like the modular aspect of AMDs (Asynchronous Module Definition, see this StackOverflow 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.
Here it is:
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()});
So 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.
A demo at http://jsfiddle.net/suKXm/
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 guys have any suggestions on any level. From the concept of such "infile" Module Loader to a hidden neat little JS feature.
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.
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.