您将看到,我有一个正在练习关闭的函数,该函数会返回另一个函数。效果很好,只是在下面的5个示例中调用函数visitsquestion(job)时, else 条件只打印出他最后一个调用函数,而我想知道如何使它们都被打印。我有以下代码:
control.route();
function interviewQuestion(job) {
return function(name) {
if (job === 'painter') {
return document.getElementById("demo1").innerHTML = name + ' how do you paint images?';
} else if (job === 'salesman') {
return document.getElementById("demo2").innerHTML = name + ' how do you sale things?';
} else if (job === 'singer') {
return document.getElementById("demo3").innerHTML = name + ' how do you sing?';
} else {
var string = '';
string = document.getElementById("demo0").innerHTML = name + ' what position are you interested in?' + '<br>';
return string;
}
}
}
//Function is called:
interviewQuestion('painter')('Lucy');
interviewQuestion('salesman')('Richard');
interviewQuestion('singer')('Juliana');
/*else*/
interviewQuestion('window washer')('Pepe');
/*else*/
interviewQuestion('bootshiner')('Bob');
如何修复 else 部分的退货声明?我希望,如果对函数的该部分进行的调用次数超过1个,则能够一次接一个地打印所有结果。
到目前为止,通过5次调用该函数,我仅获得4个结果:
答案 0 :(得分:3)
您将覆盖元素的先前值。要获取两个字符串,您可以使用+=
向其添加两个新值。
我将所有输出更改为带换行符的输出。
然后,我将return
语句更改为执行早期退出,这意味着由于else
语句已退出函数,因此您不需要return
语句。
function interviewQuestion(job) {
return function(name) {
if (job === 'painter') {
document.getElementById("demo1").innerHTML += name + ' how do you paint images?' + '<br>';
return;
}
if (job === 'salesman') {
document.getElementById("demo2").innerHTML += name + ' how do you sale things?' + '<br>';
return;
}
if (job === 'singer') {
document.getElementById("demo3").innerHTML += name + ' how do you sing?' + '<br>';
return;
}
document.getElementById("demo0").innerHTML += name + ' what position are you interested in?' + '<br>';
};
}
interviewQuestion('painter')('Lucy');
interviewQuestion('salesman')('Richard');
interviewQuestion('singer')('Juliana');
interviewQuestion('window washer')('Pepe');
interviewQuestion('bootshiner')('Bob');
<h1>Section 5: Advanced JavaScript: Objects and Functions</h1>
<h3>Practice on Closures:</h3>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo0"></p>
仅出于完整性考虑,您可以将所有信息移至对象中并为未知作业定义默认功能。
function interviewQuestion(job) {
var data = {
painter: name => document.getElementById("demo1").innerHTML += name + ' how do you paint images?' + '<br>',
painter: name => document.getElementById("demo1").innerHTML += name + ' how do you paint images?' + '<br>',
salesman: name => document.getElementById("demo2").innerHTML += name + ' how do you sale things?' + '<br>',
singer: name => document.getElementById("demo3").innerHTML += name + ' how do you sing?' + '<br>',
default: name => document.getElementById("demo0").innerHTML += name + ' what position are you interested in?' + '<br>'
};
return data[job] || data.default;
}
interviewQuestion('painter')('Lucy');
interviewQuestion('salesman')('Richard');
interviewQuestion('singer')('Juliana');
interviewQuestion('window washer')('Pepe');
interviewQuestion('bootshiner')('Bob');
<h1>Section 5: Advanced JavaScript: Objects and Functions</h1>
<h3>Practice on Closures:</h3>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo0"></p>