I have inherited the code base that I need to update for my work. I am slowly learning what they are trying to accomplish with a closure that exists, but I am stuck when I try to update part of the site using this functionality. I will give a basic plan of what the code is trying to execute, and see if anyone can help.
var TheObject = (function (){ var veryLargeDependantData = { var1: {}, var2: {}, var3: [], //Set these variables via functions function1: function f1(data){...}, function2: function f2(data){...}, initialize: function initialize() { //set values for var1... var3} }; return {initialize: veryLargeDependentData.initialize}; })().initialize();
Since I obviously cannot show the production code on the site, this will have to do. But basically the veryLargeDependentData variable is the input to the function. When the page loads, it calls the initialization function, and everything is fine. But now I need to add this to the onclick event for an older page, and the firebug console says the variable is undefined. On other pages I can use it without problems.
My question is what happens in magic, which causes the closure to not be part of the called namespace like this. I'm a bit of javascript nOOb, so I apologize if the question sounds wrong.
onclick='TheObject.initialize();'
source share