What the proper name of this Javascript pattern where a variable is assigned to value of a return function?
// array with a ton of random values.
var once = (function(){
var i = 10000, arr = [];
while(i){
arr.push( Math.random() * i );
i--;
}
arr = arr.toString();
return (function(){
return arr;
}());
}());
Edit – a better example:
var once = (function(){
// Only run a really expensive operation once...
var i = 10000, arr = [], x;
while(i){
arr.push( Math.random() * i );
i--;
}
arr = arr.toString();
x = parseFloat(arr.toString());
// then return the result of another function
return function(){
return x * (Math.random() * 10);
};
}());
$(window).resize(function(){
console.info(once());
});
I believe you’re looking for memoization.