当前位置:网站首页>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即可解决
边栏推荐
- 新产品上市最全推广方案
- ThreadPoolExecutor simple to use
- Jackson解析JSON详细教程
- Raspberry pie 4B + Intel neural computing stick (stick2) +yolov5 feasibility study report
- ODOO开发教程之透视表
- Using jupyter (I), install jupyter under windows, open the browser, and modify the default opening address
- 深度学习刷SOTA的一堆trick
- IOS interview preparation - other articles
- Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
- Mapper agent development
猜你喜欢

深度学习刷SOTA的一堆trick

网安学习-内网安全1

IDEA中使用注解Test

Download addresses of various versions of MySQL and multi version coexistence installation

Big silent event Google browser has no title
带你搞懂 Kubernetes 集群中几种常见的流量暴露方案

AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块

Five correlation analysis, one of the most important skills of data analysts

Use more flexible and convenient Rogowski coil

如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
随机推荐
Torch.nn.crossentropyloss() details
Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function
如何安装office2010安装包?office2010安装包安装到电脑上的方法
What if excel is stuck and not saved? The solution of Excel not saved but stuck
SparkSql批量插入或更新,保存数据到Mysql中
2021-10-23
P2181 diagonal
2021-10-11
Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
How to avoid damage of oscilloscope current probe
SSM integration, addition, deletion, modification and query
How to set row height and column width in excel? The method of setting row height and column width in Excel
The song of the virtual idol was originally generated in this way!
Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
JS (in ES6) sync & await understanding
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
Solve the warning prompt of MySQL mapping file
Diagram of odoo development tutorial
P1009 [noip1998 popularization group] sum of factorials
Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format