如何用break、return、continue实现JavaScript循环的跳出?

2026-04-09 21:341阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何用break、return、continue实现JavaScript循环的跳出?

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

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

前言:

一位前端界的大神让我去思考的一个问题, 给了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语句才是合法的。
阅读全文

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

如何用break、return、continue实现JavaScript循环的跳出?

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

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

前言:

一位前端界的大神让我去思考的一个问题, 给了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语句才是合法的。
阅读全文