為什麼“continue”語句在 JavaScript 中是糟糕的?


使用“continue”語句會使程式碼難以理解。在大多數情況下,使用 continue 意味著你的迴圈中的條件不足。為了避免這種情況,你可以將它新增到條件中或在迴圈中使用 if。

但是,“continue”如果使用不一致或不當,則很糟糕,否則它是一個有用的語句,可以節省大量的記憶體和程式碼行。

示例

讓我們看一個 continue 語句的示例

線上演示

<html>
   <body>
      <script>
         var x = 1;
         document.write("Entering the loop<br /> ");
         while (x < 10) {
            x = x + 1;
            if (x == 5) {
               continue; // skip rest of the loop body
            }
            document.write( x + "<br />");
         }
         document.write("Exiting the loop!<br /> ");
      </script>
   </body>
</html>

更新於: 08-Jan-2020

572 次瀏覽

開啟 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.