我们知道,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 }

转对象(含length属性)成数组Array.prototype.slice.call(arguments)的更多相关文章

  1. Array.prototype.slice.call(arguments) 类数组转成真正的数组

    Array.prototype.slice.call(arguments)   我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...

  2. JS之函数实际参数转换成数组的方法[].slice.call(arguments)

    实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组. 我们可以通 ...

  3. 理解Array.prototype.slice.call(arguments)

    在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理: 1.基本讲解 1.在 ...

  4. javascript:Array.prototype.slice.call(arguments)

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

  5. Array.prototype.slice.call(arguments)探究

    Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 首先,slice有两个用法,一个是String.slice,一个是Array.slic ...

  6. 解析Array.prototype.slice.call(arguments)

    在es5标准中,我们经常需要把arguments对象转换成真正的数组 // 你可以这样写 var arr = Array.prototype.slice.call(arguments) // 你还可以 ...

  7. 解析 Array.prototype.slice.call(arguments,0)

    Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...

  8. Array.prototype.slice.call(arguments) 通俗法理解

    Array.prototype.slice.call(arguments,num) 能将具有length属性的对象转成数组.   slice 从字面上的意思可以理解为截取数组的一部分. call 从字 ...

  9. js Array.prototype.slice.call(arguments,0) 理解

    Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...

随机推荐

  1. Android-----获取屏幕分辨率DisplayMetrics简介 .

    引自:http://blog.csdn.net/zhangqijie001/article/details/5894872 Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人 ...

  2. CenOS 用PF_RING优化Snort

    0.优化顺序 安装PF_RING的kernel模块 安装PF_RING的用户态库 安装Snort的DAQ 安装PF_RING的pfring-daq-module 安装snort 安装PF_RING-a ...

  3. backtrack种子

    下载链接:(种子文件) BT5R3-GNOME-64.torrent (md5: 8cd98b693ce542b671edecaed48ab06d) BT5R3-GNOME-32.torrent (m ...

  4. shell中break 与 continue

    在学习中我看到不单单有break和continue的存在,还有break -n  和  continue -n 的存在  那么它们有什么区别呢. 这时可以写出测设代码: for i in a b c ...

  5. BZOJ 3110:[Zjoi2013]K大数查询(整体二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3110 题意:-- 思路:其实和之前POJ那道题差不多,只不过是换成区间更新,而且是第k大不是第k小, ...

  6. 用Date.ToString()输出中英文月份

    DateTime.Now.ToString("dddd,dd MMMM,yyyy")//输出   星期三,30 一月,2008DateTime.Now.ToString(" ...

  7. iOS完美版的UIScrollView无缝循环:你值得一看

    可以直接copy运行研究 .m头文件和声明的常量(宏和const) #import "ViewController.h" // UIScrollView的尺寸 const CGFl ...

  8. HDU 5677 ztr loves substring

    Manacher+二维费用多重背包 二进制优化 这题是一眼标算....先计算出每个长度的回文串有几种,然后用二维费用的多重背包判断是否有解. 多重背包做的时候需要二进制优化. #include< ...

  9. SQL Server 2012 - Transact-SQL

    变量 --全局变量 select @@VERSION --局部变量 declare @i int set @i=5 select @i 通配符:   like   'joh%',  %任意长度的任意字 ...

  10. Animation动画

    Animation:  1,AlphaAnimation, 透明度动画, 2, RotateAnimation, 旋转动画, 3,ScaleAnimation, 缩放动画 4,TranslateAni ...