I'm examining closure in loops with this approach most popular question on closure in JS
The thing I don't understand is where I change the code of closure. I try to pass value of 'help' as an argument to closure function.
function showHelp(help) {
document.getElementById('help').innerHTML = help;
}
function makeHelpCallback(help) {
/// HERE'S THE CHANGE
return function (help) {
// instead of return function () {
showHelp(help);
};
}
function setupHelp() {
const helpText = [
{ id: 'email', help: 'Your e-mail address' },
{ id: 'name', help: 'Your full name' },
{ id: 'age', help: 'Your age (you must be over 16)' },
];
for (let i = 0; i < 1; i++) {
const item = helpText[i];
document.getElementById(item.id).onfocus = makeHelpCallback(item.help);
}
}
setupHelp();
I expected the value of 'help' to be bound to the outer scope, so I would still get three times value of "Your age (you must be over 16)". However, I get [object FocusEvent]. I don't know how it's happening.
I tried to debug this with Chrome, but to no effect.