# Storing array inside closure

Is storing array values inside of a closure considered to be bad practice? I having been trying to decide if I should just use a standard object with key value pair. This is not the method I am using to populate arrays, but this does show how i create functions to store values.

Also, I'm wondering if this is technically a closure.

http://jsfiddle.net/canoeBoy/sn3wgs0w/

<input type="button" onclick="alert(Test1(0,0,'array'));
alert(Test2(0,0,'array'));
alert(Test3(0,0,'array'));">

function makeClosures(){
var nameArr=["Test1","Test2","Test3","Test4","Test5","Test6","Test7"];
for(var i=0;i<nameArr.length;i+=1){
var nam=nameArr[i];
window[nam]=funcsRun(nam,0,1);
}
}

function funcsRun(nam,index,action){
var a=nam;
var arr=[];

return function(value,index,action) {
if(action=='array'){

}
else if(action=='copyEntireArray'){
arr=value;
}
else if(action=='resetArray'){
arr=[];
}
else if(action=='copyToIndex'){
arr[index]=value;
}

else if(action=='indexValue'){
return arr[index];
}
else if(action=='push'){
arr.push(value);
}
return arr;
};
};

makeClosures();

for(i=0;i<100;i+=1){
Test1(Math.floor((Math.random() * 100) + 1),i,'copyToIndex');
Test2(Math.floor((Math.random() * 100) + 1),i,'copyToIndex');
Test3(Math.floor((Math.random() * 100) + 1),i,'copyToIndex');

}

• What is the use case of this? I don't see how this is useful... Is this real-world code? – elclanrs Aug 26 '14 at 2:15
• Need a way of storing data relating to hundreds of elements. Name, ID, video links, image links, coordinates on page, etc. I don't want to have to pull data from server every time it is needed. Instead I am saving data in an HTML file that can be opened in iframe then processed by main page. Data can also be stored on DB as text blob, but I was thinking might be faster page load to just access HTML page. – rancho Aug 26 '14 at 2:26

## 1 Answer

Okay, your question is very ambiguous, but I'll give it a shot. First, is seems like you're confused about what a closure is. A closure is simply accessing a variable that was not defined inside that function.

For instance, in:

function foo () {
var bar = 9;
function bla () {
return bar;
}
}


bar can be accessed because of closure. Read this if you still have questions about that.

Now regarding your use case, it seems like what you want is a cache. You don't want to go to the server every time to get the same information, you want the user to get information that has already been requested. That's the use case of a cache.

This is how you create a cache in JavaScript:

var cache = {};


Now you need a way to index your data. Since it seems like you get it from a db, you'll have an ID available, index it by that. So your app will look something like:

\$.ajax(....).done(function (data) {
// Cache your data
cache[data.id] = data;
});


Now before every db request you can check if the data is in the cache or not. If it is you can use the copy in the cache, if not issue the request to the server.