共计 820 个字符,预计需要花费 3 分钟才能阅读完成。
1.
function showHelp(help) {
document.getElementById(‘help’).innerHTML = help;
}
function makeHelpCallback(help) {
return function() {
showHelp(help);
};
}
function setupHelp() {
var 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 (var i = 0; i < helpText.length; i++) {
var item = helpText[i];
document.getElementById(item.id).onfocus = makeHelpCallback(item.help);
}
}
2.
var funcs = [];
for (var i = 0; i < 3; i++) {// let’s create 3 functions
funcs[i] = function() { // and store them in funcs
console.log(“My value: ” + i); // each should log its value.
};
}
for (var j = 0; j < 3; j++) {
funcs[j](); // and now let’s run each one to see
}
VS
var funcs = [];
function createfunc(i) {
return function() { console.log(“My value: ” + i); };
}
for (var i = 0; i < 3; i++) {
funcs[i] = createfunc(i);
}
for (var j = 0; j < 3; j++) {
funcs[j](); // and now let’s run each one to see
}