为什么下划线说 _.initial 对参数对象特别有用

why underscore says _.initial is Especially useful on the arguments object

本文关键字:对象 有用 参数 下划线 initial 为什么      更新时间:2023-09-26

我正在阅读下划线的源代码,我在 _.initial 中找到了一条注释

// Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N. The **guard** check allows it to work with
// `_.map`.
_.initial = function(array, n, guard) {
return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
};

我很好奇 _.initial 在哪种情况下对参数对象有用

从概念上讲,arguments对象是一个数组,但arguments的类型是object(不是数组),数组函数(如切片)未在参数对象的原型中定义。下划线中有许多帮助程序方法可以解决此问题,即提供适用于数组和参数对象的实用程序函数。

说这对参数对象有用的注释目的是基于下划线 js 中数组和参数对象的这种统一。