I try to learn javascript and therefore I tried the following code:
function repeat(n, action) {
for (var i = 0; i < n; i++) {
action();
}
}
function benchmark() {
var start = [], end = [], timings = [];
repeat(1000, function() {
start.push(Date.now());
// Repeat something
end.push(Date.now());
});
for (var i = 0; i < end.length; i++) {
timings[i] = end[i] - start[i];
}
return timings;
}
My Question now: Why can I access start and end in my callback? As I understood the scope changed from benchmark to repeat. Is the callback a closure or is a closure onle a defined named function in another function?
Thanks!