var GlobalObject = (function(){
var arr1 = [];
var arr2 = [];
var arr3 = [];
var arr4 = [];
return {
}
})();
- Now how would i call GlobalObject.arr1 across the application?
- Will it maintain its reference and value, when i do a push and pop.
- Will this cause any memory leak
arr1is defined only within the scope of theGlobalObjectfunction. If you want to access it globally, you need to have theGlobalObjectfunction return a reference to it. Read up on functional scope in javascript for a better understanding of this. Currently your function returns an empty object. You want to do something like this:(though you can maintain the privacy of those arrays by not returning them)
The
GlobalObjectwill maintainarr1‘s reference and value until you manually de-reference it (setting it tonull). Javascript’s garbage collector only deletes objects that aren’t needed anymore.Your current code shows no memory leaks. Watch out for circular references and closures.