為什麼 JavaScript 中的“continue”語句不好?
“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>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP