← 返回完整题库
JavaScript简单

Array.prototype.slice.apply(arguments) 的含义

参考答案

把 slice 方法借给 arguments 使用,得到一个真正的数组(浅拷贝)。

拆解:

  • slice 内部只依赖 length 与数字索引,因此可以作用在类数组上;
  • .apply(arguments) 把 arguments 作为 this 传入(同时可传参区间);
  • 效果等价 Array.from(arguments) / [...arguments]。

用途:ES6 前把 arguments 转数组以使用数组方法;现代可直接用 rest 参数 (...args)。

本题来自 计算机知识面试题库,共 921 道题。