For architecture and namespacing purposes, I want to do this:
function outer (arr) {
function inner(arrElement) {
return doStuffTo(arrElement);
}
var results = [];
arr.forEach(element, index, array) {
results.push(inner(element));
}
return results;
}
So basically, a function within a function. Simple stuff. But outer() is something that will be executed a lot. Does this mean the overhead of defining a function (on top of evaluating it) will apply every time outer() is called? For this to be efficient, must I define inner() outside?
You could use a closure:
inner is held in a closure and remains “private” to outer, and is only created once when outer is initialised.