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

javascript:Array.prototype.slice.call(arguments)的更多相关文章

  1. [转] 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)

    假如你是一个 JavaScript 开发者,你可能见到过 Array.prototype.slice.apply(arguments) 这样的用法,然后你会问,这么写是什么意思呢? 这个语法其实不难理 ...

  2. [转载]Array.prototype.slice.call(arguments,1)原理

    Array.prototype.slice.call(arguments,1)该语句涉及两个知识点. arguments是一个关键字,代表当前参数,在javascript中虽然arguments表面上 ...

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

    Array.prototype.slice.call(arguments)能够将具有length属性的对象转化为数组, 可以理解为将arguments转化成一个数组对象,让它具有slice方法 如: ...

  4. 详解 Array.prototype.slice.call(arguments)

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

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

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

  6. 转对象(含length属性)成数组Array.prototype.slice.call(arguments)

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

  7. js基础进阶--关于Array.prototype.slice.call(arguments) 的思考

    欢迎访问我的个人博客:http://www.xiaolongwu.cn Array.prototype.slice.call(arguments)的作用为:强制转化arguments为数组格式,一般出 ...

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

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

  9. Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)

    Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...

随机推荐

  1. phantomhs获取网页的高度

    function heheda() { window.setTimeout(function () { console.log("---------------------Capture O ...

  2. ORACLE数据库数据文件转移方法(不同于move方法)

    1) 手动拷贝要转移的数据数据文件'd:\OracleData\GWTABLE42.DBF'到新的位置'E:\OracleData\GWTABLE42.DBF'. 2) 把数据文件所属的表空间Offl ...

  3. angular有关网站

    angular官网 https://v2.angular.cn/docs/ts/latest/ angular更新信息https://github.com/angular/angular/blob/m ...

  4. Java基础90 MySQL触发器

    1.创建触发器 CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_stmt ...

  5. libsvm的使用

    参考:http://www.cnblogs.com/GuoJiaSheng/p/4480497.html http://www.cnblogs.com/tornadomeet/archive/2012 ...

  6. Windows自动联网修复系统损坏文件

    sfc修复: Win+X---命令提示符(管理员)---sfc /scannow---Enter可以查看是否存在系统文件损坏等问题 以管理员权限运行命令提示符,执行“dism /online /cle ...

  7. hdu 5441 (2015长春网络赛E题 带权并查集 )

    n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排 ...

  8. poj 1611 求0号结点所在集合的元素个数

    求0号结点所在集合的元素个数 Sample Input 100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0Sample Out ...

  9. 为什么不要在Spring的配置里,配置上XSD的版本号

    为什么不要在Spring的配置里,配置上XSD的版本号?因为如果没有配置版本号,取的就是当前jar里的XSD文件,减少了各种风险.而且这样约定大于配置的方式很优雅.

  10. 图学ES6-2.let与const命令