Sign in
Log inSign up
An object and scope problem

An object and scope problem

Robert van der Elst's photo
Robert van der Elst
·Jan 12, 2016

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?