为什么 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