Array.prototype.slice.call(arguments)

本文深入探讨了使用Array.prototype.slice.call()方法将类数组对象转换为真正的数组的技术细节,并提供了适用于不同浏览器环境的通用函数。

我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换)

如:

1 var a={length:2,0:'first',1:'second'};
2 Array.prototype.slice.call(a);//  ["first", "second"]
3  
4 var a={length:2};
5 Array.prototype.slice.call(a);//  [undefined, undefined]

可能刚开始学习js的童鞋并不是很能理解这句为什么能实现这样的功能。比如我就是一个,所以,来探究一下。

首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组,这里我们看第2个。

Array.prototype.slice.call(arguments)能够将arguments转成数组,那么就是arguments.toArray().slice();到这里,是不是就可以说Array.prototype.slice.call(arguments)的过程就是先将传入进来的第一个参数转为数组,再调用slice?

再看call的用法,如下例子

1 var a = function(){
2      console.log(this);    // 'littledu'
3      console.log(typeof this);      //  Object
4      console.log(this instanceof String);    // true
5 }
6 a.call('littledu');

可以看出,call了后,就把当前函数推入所传参数的作用域中去了,不知道这样说对不对,但反正this就指向了所传进去的对象就肯定的了。

到这里,基本就差不多了,我们可以大胆猜一下slice的内部实现,如下

1 Array.prototype.slice = function(start,end){
2      var result = new Array();
3      start = start || 0;
4      end = end || this.length; //this指向调用的对象,当用了call后,能够改变this的指向,也就是指向传进来的对象,这是关键
5      for(var i = start; i < end; i++){
6           result.push(this[i]);
7      }
8      return result;
9 }

大概就是这样吧,理解就行,不深究。

最后,附个转成数组的通用函数

 1 var toArray = function(s){
 2     try{
 3         return Array.prototype.slice.call(s);
 4     } catch(e){
 5             var arr = [];
 6             for(var i = 0,len = s.length; i < len; i++){
 7                 //arr.push(s[i]);
                   arr[i] = s[i];  //据说这样比push快
 8             }
 9              return arr;
10     }
11 }
在 JavaScript 中,类数组对象(如 `arguments` 对象或 DOM 操作返回的 `NodeList`)不具备数组的方法,例如 `slice()`、`map()` 等。为了将这些类数组结构转换为真正的数组以便使用数组方法,开发者常常使用 `Array.prototype.slice.call()`。 ### 作用 `Array.prototype.slice.call(arguments)` 的作用是将一个类数组对象转换为一个新的数组实例。这种技术利用了函数的 `.call()` 方法来改变 `slice()` 函数内部 `this` 的指向,使其可以操作非数组的对象。由于 `slice()` 本身只需要访问对象的索引和 `length` 属性,因此任何具有这两个特性的对象都可以被“切片”成数组。 通过这种方式,`arguments` 对象可以像数组一样进行操作,比如遍历、过滤等[^3]。 ### 使用方法 基本语法如下: ```javascript Array.prototype.slice.call(arrayLikeObject, start, end); ``` - `arrayLikeObject`:要转换的类数组对象。 - `start`(可选):开始索引,默认为 `0`。 - `end`(可选):结束索引(不包含该索引对应的元素),默认为 `this.length`。 #### 示例 1. **将 `arguments` 转换为数组** ```javascript function example() { const args = Array.prototype.slice.call(arguments); console.log(args); // 输出: [1, 2, 3] } example(1, 2, 3); ``` 2. **将 `NodeList` 转换为数组** ```javascript const divs = document.querySelectorAll('div'); const divArray = Array.prototype.slice.call(divs); console.log(divArray); // 输出: [div, div, div, ...] ``` 3. **从特定索引开始转换** ```javascript function example() { const args = Array.prototype.slice.call(arguments, 1); console.log(args); // 输出: [2, 3] } example(1, 2, 3); ``` 4. **替代写法:使用 `[].slice.call()`** 除了完整的 `Array.prototype.slice.call(...)` 写法,也可以简写为: ```javascript const args = [].slice.call(arguments); ``` 这种方式更为简洁,并且功能完全一致[^2]。 ### 原理简析 `slice()` 方法本质上并不依赖于对象是否是真正的数组,而是检查其是否具备 `length` 属性以及可以通过索引访问元素。因此,只要传入的对象满足这些条件,`slice()` 就可以将其转换为数组。通过 `.call()` 或 `.apply()`,可以动态地将 `this` 绑定到目标对象上,从而实现类数组对象的“数组化”处理[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值