ES5时代的Array.prototype.slice.call(arguments)已成为历史,现代JS开发应优先采用语义清晰、性能更优的剩余参数语法...args。

在ES5时代,开发者常用Array.prototype.slice.call(arguments)处理函数参数,如今这种写法已经过时。判断其是否属于老旧代码,主要观察三个特征:是否在传统函数中手动转换arguments、是否使用.call()借用方法、以及是否完全未使用ES6+语法特性。若发现此类代码,通常意味着需要进行现代化改造。
这种传统写法通常具备以下特点:
const args = Array.prototype.slice.call(arguments)或[].slice.call(arguments)function声明而非箭头函数,且内部依赖arguments对象...、Array.from、for...of等现代语法特性剩余参数语法是目前最推荐、性能最优的替代方案,它能直接生成真正的数组对象。
function sum() { const args = Array.prototype.slice.call(arguments); ... }function sum(...args) { /* args可直接使用 */ }const sum = (...args) => args.reduce((a, b) => a + b, 0);当无法直接修改参数签名时(如封装现有函数或处理非arguments的类数组对象),可考虑以下方案:
length: undefined等特殊情况,支持NodeList、HTMLCollection甚至Map/Set的键值转换Symbol.iterator;注意旧版DOM中部分HTMLCollection不可迭代可能报错[...document.querySelectorAll('div')],相比Array.from()更加轻量,适合日常DOM操作场景这种写法虽非错误,但已不合时宜:
arguments.length为NaN时会静默返回空数组,行为不可预测arguments对象)...和Array.from进行深度优化,性能差异可忽略不计升级老旧写法不仅能提升代码可读性,还能避免潜在兼容性问题,是每个现代JS开发者的必备技能。