We want to create a function, which returns an array of functions, which return their index in the array. For better understanding, here an example:
var callbacks = createFunctions(5); // create an array, containing 5 functions
callbacks[0](); // must return 0
callbacks[3](); // must return 3
We already implemented that function, but when we actually run the code, the result doesn't look like what we expected. Can you spot, what's wrong with it? A test fixture is also available
************************
Пример:
function createFunctions(n) {
var callbacks = [];
for (var i=0; i<n; i++) {
callbacks.push(function() {
return i;
});
}
return callbacks;
}
Код вернет массив функций. каждая функция вернет 5.
Не могу понять, как анонимная функция связана со областью видимости createFunctions(n)
Почему в Масив вернется не чисто анонимная функция а еще и зацепка за createFunctions(n)?