arguments 是类数组对象,需转为真数组才能调用数组方法;ES6+ 推荐扩展运算符 [...arguments],兼容环境可用 Array.prototype.slice.call(arguments) 或 Array.from(arguments)。

arguments 是函数内部的类数组对象,有 length 和数字索引,但不能直接调用 push、map、reduce 等数组方法。要让它具备数组能力,必须转为真数组。现代和兼容性兼顾的方案有几种,关键看运行环境和具体需求。
ES6+ 推荐:扩展运算符(…)
最简洁直观,适用于所有可迭代的类数组对象(包括 arguments):
- 写法:
const args = [...arguments]; - 优点:语义清晰、性能好、支持链式操作(如
[...arguments].map(...)) - 注意:仅在严格模式或非箭头函数中可用;箭头函数没有
arguments,需改用剩余参数...args
兼容老环境:Array.prototype.slice.call()
长期稳定、IE9+ 全支持,原理是借用数组的 slice 方法处理类数组结构:
- 写法:
const args = Array.prototype.slice.call(arguments);或简写为[].slice.call(arguments) - 原理:
slice不校验this是否为数组,只要对象有length和数字索引,就能按序拷贝成新数组 - 常见误写:
Array.slice.call(arguments)会报错,因为Array是构造函数,slice在其原型上
通用灵活:Array.from()
专为类数组/可迭代对象设计,语法干净,还支持映射处理:
- 写法:
const args = Array.from(arguments); - 进阶用法:
Array.from(arguments, x => x * 2)可在转换时直接变形 - 兼容性:Chrome 45+/Firefox 32+/Safari 9+,不支持 IE;若需 IE 支持,需搭配 polyfill
不推荐的手动遍历写法
虽可行,但冗长易错,且无必要:
- 例如:
for (let i = 0; i - 问题:比内置方法慢;未处理稀疏索引;容易漏掉边界(如
length === 0) - 除非极端受限环境(如无任何 ES5 支持),否则无需采用


















