当前位置:网站首页>js(forEach)出现return无法结束函数的解决方法
js(forEach)出现return无法结束函数的解决方法
2022-07-29 05:06:00 【nianyuw】
js(forEach)出现return无法结束函数的解决方法
今天有人问了一个问题,return表示直接结束(终止)函数么?我说没错,他给我抛出一个问题

他在这里的return并没有按我们想象的那样结束函数,代码继续向下执行了

我就去度娘研究了一下,发现在js 函数多层调用时候无法整体return
解决方法一:
先来一个变通的方法(在需要停止的代码区域中抛出异常,即可停止代码运行)

throw console.info('内容');
throw "内容";
解决方法二:
在forEach中,执行return是无法结束整个方法的(forEach 无法在所有元素都传递给调用的函数之前终止遍历)
换成for循环,执行return即可解决
边栏推荐
- How does word view document modification traces? How word views document modification traces
- Glory 2023 push, push code ambubk
- On prepayment of house purchase
- How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
- Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
- ThreadPoolExecutor simple to use
- 虚拟偶像的歌声原来是这样生成的!
- How to add traffic statistics codes to the legendary Development Zone website
- Mysql:the user specified as a definer ('root '@'%) does not exist
- Numpy basic learning
猜你喜欢

Big silent event Google browser has no title

Unity metaverse (III), protobuf & socket realize multi person online

开源汇智创未来 | 2022开放原子全球开源峰会 openEuler 分论坛圆满召开

C language implementation of three chess

自贸经济中架起的“隐形桥梁”:国货精品与中国AI力量

How is the entered query SQL statement executed?

Data Lake: spark, a distributed open source processing engine
带你搞懂 Kubernetes 集群中几种常见的流量暴露方案

stack和queue和优先级队列(大堆和小堆)模拟实现和仿函数讲解

Excel怎么筛选出自己想要的内容?excel表格筛选内容教程
随机推荐
JS (in ES6) sync & await understanding
Glory 2023 push, push code ambubk
[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
Google GTEST event mechanism
Numpy basic learning
The difference between the two ways of thread implementation - simple summary
Data Lake: spark, a distributed open source processing engine
What are the core features of the digital transformation of state-owned construction enterprises?
输入的查询SQL语句,是如何执行的?
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
Mapper agent development
MySQL time calculation function
Use annotation test in idea
开区网站打开自动播放音乐的添加跟修改教程
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Double type nullpointexception in Flink flow calculation
带你搞懂 Kubernetes 集群中几种常见的流量暴露方案
Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function
学习数据库的第一个程序
金达威董秘回复:公司看好NMN产品的市场前景,已推出系列产品