现代推荐用剩余参数(...args)替代array.prototype.slice.call(arguments),因其更简洁、语义清晰且避免arguments的限制;次选展开运算符[...arguments]或array.from(arguments)。

现在直接用扩展运算符 [...arguments] 就能替代 Array.prototype.slice.call(arguments),既简洁又语义清晰。
为什么老写法难读
Array.prototype.slice.call(arguments) 本质是借用数组方法处理类数组对象,但写法绕、术语多(prototype、call、slice),新人容易卡在“为什么不能直接 arguments.slice()”。它还隐含了对 arguments 对象特性的依赖,而该对象在箭头函数或严格模式下行为更受限。
现代写法:展开运算符直接解构
在函数体内,把 arguments 展开成真正的数组:
- ✅ [...arguments] —— 最常用,一行搞定,可读性高
- ✅ Array.from(arguments) —— 语义明确,兼容性略好(支持 IE11),适合需额外处理的场景(如映射)
- ✅ 改用 剩余参数(rest parameters) —— 更推荐,从源头避免
arguments:function sum(...nums) { return nums.reduce((a, b) => a + b, 0); }
实际替换示例
旧写法:
function logAll() {<br> var args = Array.prototype.slice.call(arguments);<br> console.log(args);<br>}
新写法(三选一):
- 展开运算符:
const args = [...arguments]; - Array.from:
const args = Array.from(arguments); - 剩余参数(最优):
function logAll(...args) { console.log(args); }
注意边界情况
剩余参数无法获取命名参数之后的“动态”参数以外的信息(比如原生函数的 length 或 caller),但绝大多数业务场景不需要这些。如果真需访问 arguments.callee(已废弃)或做参数重绑定,说明设计可能需要重构,而不是坚持旧模式。











