【问题标题】:Why are different results when I call these functions?为什么我调用这些函数时结果不同?
【发布时间】:2019-04-26 10:27:46
【问题描述】:

我使用过很多次 JAVA,但之前从未使用过 Javascript。我尝试了解它,现在我对这些功能有疑问。相同的功能具有不同的结果。此行函数之间的唯一区别:return joe;

"rowSearch()" 函数中,这段代码在“循环”之外,而在"rowSearch2()" 中,这段代码在“循环”之内。

如果我调用 rowSearch2(),那么 JOE 变量每次都有返回值,但是当我调用 rowSearch() 函数时,有时没有任何返回值。也许我错过了或者我做错了什么,但我看不到。

非常感谢。

function rowSearch() {
  var index, table = document.getElementById('table');
  var joe = 1;
  joe = 1;
  var temp = '';
  var temp2 = '';
  for (var j = 1; j < table.rows.length + 1; j++) {
    temp = table.rows[j].cells[0].innerHTML;
    temp2 = document.getElementById('srch').value;

    if (temp == temp2) {
      j = table.rows.length + 1;
      joe = 0;
    }
  }
  return joe;
}

function rowSearch2() {
  var index, table = document.getElementById('table');
  var joe = 1;
  joe = 1;
  var temp = '';
  var temp2 = '';
  for (var j = 1; j < table.rows.length + 1; j++) {
    temp = table.rows[j].cells[0].innerHTML;
    temp2 = document.getElementById('srch').value;

    if (temp == temp2) {
      j = table.rows.length + 1;
      joe = 0;
    }

    return joe;
  }
}

<!DOCTYPE html>
<html>
    <head>

        <script>

          function rowSearch() {
  var index, table = document.getElementById('table');
  var joe = 1;
  joe = 1;
  var temp = '';
  var temp2 = '';
  for (var j = 1; j < table.rows.length + 1; j++) {
    temp = table.rows[j].cells[0].innerHTML;
    temp2 = document.getElementById('srch').value;

    if (temp == temp2) {
      j = table.rows.length + 1;
      joe = 0;
    }
    document.getElementById('demo4').innerHTML=joe;
  }

}
        </script>

    </head>

    <body>

        Data: <input type="text" name="srch" id="srch" /><br/><br/>
        <button onclick="rowSearch();">Check</button><br/><br/>

        <table id="table" border="1">

            <tr>
                <th>Data</th>


            </tr>
            <tr>
                <td>Data1</td>


            </tr>
            <tr>
                <td>Data2</td>


            </tr>
            <tr>
                <<td>Data3</td>


            </tr>



        </table>
        <p id="demo"></p>
        <p id="demo2"></p>
        <p id="demo3"></p>
        <p id="demo4"></p>

 </script>
    </body>

</html>

我想在表格中找到一个值(srch 输入)。如果它在表中,那么我将调用一个值为 JOE 的函数,否则调用另一个函数。

【问题讨论】:

  • 能否也添加相应的HTML脚本,并制作一个sn-p,以便调试?
  • return in 循环将终止它。因此,您的rowSearch2() 将在一次迭代后终止循环并将joe 的值发送给调用函数。在您的第一个函数 rowSearch() 中,您允许完成 for 循环,然后将 joe 的值发送给调用函数。

标签: javascript


【解决方案1】:

正如@randomSoul 已经提到的,这两个函数并不相等,但据我所知,JAVA 在这里的行为确实相同。

函数内部某处的return 语句会立即停止该函数并返回一个值;甚至在一个循环中。

所以,你的两个函数都是伪代码

function rowSearch() {
  if the `innerHTML` of the first cell in **any row (except the first)** of the table equals `srch`
  then return 0 
  else return 1
}

我假设第一行是你的表头

function rowSearch2() {
  if the table has at least two rows
  then if the `innerHTML` of the first cell in **the second row** of the table equals `srch`
    then return 0 
    else return 1
  else return undefined
}

这里 JS 和 Java 之间的唯一区别是,在 JS 中 rowSearch2 将在没有行时隐式 return undefined ,而 Java 会抛出错误。 或者甚至可能无法编译,因为并非函数的每个路径都返回一个值。 Java 不喜欢这种模棱两可 ;)

【讨论】:

  • 是的,这两个函数不一样,你也可以删除//joe = 1;,因为你已经在初始化var joe =1.. 并不是说​​它可能对你的执行有任何影响
猜你喜欢
  • 1970-01-01
  • 2015-03-11
  • 2022-11-21
  • 2012-08-07
  • 1970-01-01
  • 2018-01-14
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多