Hotline:15911225507
Long Xiao
Login
Home-All Questions-Javascript-Main Content

Javascript 为什么arguments是一个类数组?用什么方法遍历类数组?

龙霄龙霄
Javascript
3 years ago
0
0
2.01K

因为arguments是一个对象,他的属性是从0依次增大的数字,并且不能使用数组的方法,所以它是一个类数组。

(1)用call()或apply()方法先将类数组转换成真正的数组

Array.prototype.forEach.call(arguments,a=>console.log(a))

(2)用Array.from()方法将数组转换成真正的数组

const arr = Array.from(arguments);
arr.forEach()

(3)利用扩展运算符转换为真数组

[…arguments].forEach()
龙霄

龙霄

Lv1Vip2Rec2
以 Nuxt.js 之力,焕新 WordPress 体验
From: Course-JavaScript 教程
13.98W1W
Loading...
Share:
Long Xiao
No answers yet, be the first?
我要回答
Accepted answer rewards 100 points
Long Xiao
Please login first