Uncaught TypeError: arguments.forEach is not a function

今天复习arguments,参考pink老师的视频。

在demo里,我打算使用arguments对参数进行一个遍历,习惯性的用了forEach,万万没想到居然报错了。

 

 经过仔细查看arguments的属性才知道,arguments是个类数组,虽然具有数组的部分特性,但毕竟不是真正的数组,所以不能用forEach,倒是可以用for。

但由于个人原因,在这里需要用forEach,所以去查询和请教其他解决方案,结果还真让我找到了几种,方案如下:

1、使用Array.prototype.slice.call(arguments),将arguments转为数组,再去遍历

原文链接:javascript - JS arguments.forEach is not a function - Stack Overflow

 

 

 2、使用解构+转换的方式,将arguments转为数组,再去遍历

 

好啦,以上就是直接使用arguments.forEach()时报错的解决方案啦,希望对兄弟们有所帮助。

猜你喜欢

转载自blog.csdn.net/weixin_44016350/article/details/127510685