I'm designing the structure of an HTML5 online videogame, and I want to use some namespaces in order to organize all my functions and classes, so this idea just came to me a few mins ago, but I don't know if it is the best:
(
function ()
{
var xhr = new XMLHttpRequest();
function load (file)
{
xhr.open('GET', file, false);
xhr.send();
return xhr.responseText;
}
eval // Imported files
(
load('func.js') +
load('class.js')
);
function main ()
{
func();
var instance = new Class();
alert(instance.getMessage());
}
window.addEventListener('load', main, false);
}
)();
func.js:
function func ()
{
alert('This is an imported function.');
}
Class.js
function Class ()
{
this.getMessage = function ()
{
return "This is an imported class.";
}
}
I like this because it looks like the file import model from PHP, and I avoid so many verbose things when I need to call each function, but I'm not that sure, what do you think about it?
eval. This doesn't seem like good practice. Look into RequireJS and Browserify.