如何在闭包中连接变量字符串

奥古斯丁

正如您将看到的,我有一个函数,在其中我练习一个带有返回另一个函数的函数的闭包它工作正常,只是在下面的5个示例中调用函数visitsquestion(job)时,else条件仅打印出他最后一个调用函数,而我想知道如何使它们都被打印。我有以下代码:

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');
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Section 5: Advanced JavaScript: Objects and Functions</title>
</head>

<body>
  <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>

</body>

</html>

我该如何修复else部分的退货声明我希望,如果对该函数的该部分进行多次调用,则能够一次接一个地打印所有结果。

截至目前,通过对该函数的5次调用,我仅得到4个结果:

  • 露西你怎么画图像?
  • 理查德你怎么卖东西?
  • 卡拉你怎么唱歌?
  • 鲍勃,您对什么职位感兴趣?
妮娜·斯科茨(Nina Scholz)

您将覆盖元素的先前值。要获取两个字符串,您可以使用为其添加两个新值+=

我将所有输出更改为带换行符的输出。

然后,我将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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章