JavaScript中如何使用break、return和continue实现循环退出?

更新于
2026-10-01 06:45:59
0阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计863个文字,预计阅读时间需要4分钟。

JavaScript中如何使用break、return和continue实现循环退出?

前言:一位前端大神让我去思考的一个问题,给了Big-man一段代码,如下:

javascriptfunction Seriously(options) { // if called without 'new', make a new object and return that if (!(this instanceof Seriously)) { return new Seriously(options); }}

前言:

一位前端界的大神让我去思考的一个问题, 给了Big-man一段代码,如下:

function Seriously(options) { // if called without 'new', make a new object and return that if(window === this || !(this instanceof Seriously) || this.id !== undefined) { return new Seriously(options); } }

return语句执行之后还会继续执行吗?这是大神上来让我解决的问题,既然提到了return那我也就随带解决JS中另外的两种结束循环的方法break, continue。

Break语句:

  • break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch语句。
  • 由于它是用来退出循环或者switch语句的, 所以只有当它出现在这些语句的时候, 这种形式的break语句才是合法的。
阅读全文

本文共计863个文字,预计阅读时间需要4分钟。

JavaScript中如何使用break、return和continue实现循环退出?

前言:一位前端大神让我去思考的一个问题,给了Big-man一段代码,如下:

javascriptfunction Seriously(options) { // if called without 'new', make a new object and return that if (!(this instanceof Seriously)) { return new Seriously(options); }}

前言:

一位前端界的大神让我去思考的一个问题, 给了Big-man一段代码,如下:

function Seriously(options) { // if called without 'new', make a new object and return that if(window === this || !(this instanceof Seriously) || this.id !== undefined) { return new Seriously(options); } }

return语句执行之后还会继续执行吗?这是大神上来让我解决的问题,既然提到了return那我也就随带解决JS中另外的两种结束循环的方法break, continue。

Break语句:

  • break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch语句。
  • 由于它是用来退出循环或者switch语句的, 所以只有当它出现在这些语句的时候, 这种形式的break语句才是合法的。
阅读全文