Reputation: 23634
var GlobalObject = (function(){
var arr1 = [];
var arr2 = [];
var arr3 = [];
var arr4 = [];
return {
}
})();
Upvotes: 1
Views: 303
Reputation: 45807
arr1
is defined only within the scope of the GlobalObject
function. If you want to access it globally, you need to have the GlobalObject
function 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:
...
return {
"array1": arr1,
"array2": arr2,
"array3": arr3,
"array4": arr4
};
(though you can maintain the privacy of those arrays by not returning them)
The GlobalObject
will maintain arr1
's reference and value until you manually de-reference it (setting it to null
). 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.
Upvotes: 2
Reputation: 413996
arr1
is exposed as a value of a property of the returned objectFor (1), you'd have to make sure that one of the properties of the object returned is a reference to the array:
// ...
return {
whatever: arr1,
// ...
};
As to memory leaks, there's nothing about a setup like this that's particularly problematic.
Upvotes: 1