so, I've been rewriting my Javascript (jQuery) file with a module pattern, which works great.
But I'm having a problem with an external object which I need for translations. These will come from an external Javascript file which is dynamically generated.
Below a quick demo of what I have now:
// languageobj will be a separate file
var languageobj = {
param1: 'string',
param2: 'string'
}
// The anonymous function will contain all JS needed for the site
(function() {
var Modules = {
// I need to reference the languageobj here, something like this:
$(this).append('<a href="#">' + languageobj.param1 + '</a>');
}
}());
If I do it like above, I get multiple errors telling me that languageobj isn't available... If I move the code for languageobj inside the anonymous function it works (as expected), but I need languageobj outside of the anonymous function. Any ideas how to reference languageobj from within the anonymous function?
You can pass arguments to your module's anonymous function (also known as mixins).
I've made a simple example, you can check here.
You can also read Addy Osmani's explanation of Importing Mixins under module pattern variations section.
Jędrzej Chałubek
There is an error in demo. You can't declare
Modulesobject that way.Use self-invoked anonymous function and try to pass languageobj as param of anonymous function. jsbin.com/robakakoja/1/edit
If
languageobjis in global scope you should access it inside anonymous function without passing as param. See: jsbin.com/rugarajitu/1/edit