Array.prototype.slice.call(arguments)

 
我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换)
如:
 var a={length:2,0:'first',1:'second'};
Array.prototype.slice.call(a);// ["first", "second"] var a={length:2};
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的用法,如下例子
 var a = function(){
console.log(this); // 'littledu'
console.log(typeof this); // Object
console.log(this instanceof String); // true
}
a.call('littledu');
可以看出,call了后,就把当前函数推入所传参数的作用域中去了,不知道这样说对不对,但反正this就指向了所传进去的对象就肯定的了。
到这里,基本就差不多了,我们可以大胆猜一下slice的内部实现,如下
 Array.prototype.slice = function(start,end){
var result = new Array();
start = start || 0;
end = end || this.length; //this指向调用的对象,当用了call后,能够改变this的指向,也就是指向传进来的对象,这是关键
for(var i = start; i < end; i++){
result.push(this[i]);
}
return result;
}

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

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

 var toArray = function(s){
try{
return Array.prototype.slice.call(s);
} catch(e){
var arr = [];
for(var i = 0,len = s.length; i < len; i++){
//arr.push(s[i]);
arr[i] = s[i]; //据说这样比push快
}
return arr;
}
}

转自http://www.cnblogs.com/littledu/archive/2012/05/19/2508672.html

Array.prototype.slice.call(arguments) 类数组转成真正的数组的更多相关文章

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

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

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

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

  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.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 ...

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

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

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

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

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

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

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

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

随机推荐

  1. java.sql.DataTruncation: Data truncation

    问题一: 用jdbc插入数据时,当数据库表中某列设置的长度小于要插入的字符的实际长度时就会抛出此异常. 如:数据表中某列char [4],长度为4,插入:"Hello"时,字符&q ...

  2. 探秘Java虚拟机——内存管理与垃圾回收

    本文主要是基于Sun JDK 1.6 Garbage Collector(作者:毕玄)的整理与总结,原文请读者在网上搜索. 1.Java虚拟机运行时的数据区 2.常用的内存区域调节参数 -Xms:初始 ...

  3. jwplayer 网页在线播放插件

    1.到官网 https://www.jwplayer.com/ 注册,取得key并下载免费版本(免费版只支持mp4格式): 2.编辑如下网页即可在线播放: <!DOCTYPE html> ...

  4. linux中的设备名称和设备号

    看赵炯博士的<linux 0.11 源代码注释>已经两三周了,从今天起开始将一些个人总结和感悟分小标题写出来,聊作记忆以供后来查看.在linux0.11源码的 /linux/boot/bo ...

  5. List<HashMap>和HashMap

      例如select查询出的是学号.姓名,比如查出符合条件的是学号是0810的小红,学号是0811的小明,组织起来如下:  list.add(hashmap1);list.add(hashmap2); ...

  6. error MSB3027: Could not copy "xxx.dll" to "xxx.dll". Exceeded retry count of 10. Failed.

    错误提示内容: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3363,5): error MSB302 ...

  7. Diamond

    Diamond主要提供持久配置的发布和订阅服务,最大特点是结构简单,稳定可靠.Diamond的主要使用场景是用来进行动态数据库切换与扩容,进行一些业务系统运行时开关配置的推送.Diamond产品专注于 ...

  8. CentOS 6.4利用xampp安装bugfree3

    1.下载xampp 安装 http://www.apachefriends.org/zh_cn/xampp.html 直接执行.run文件安装  默认会安装到/opt/lampp 2 .启动xampp ...

  9. leetcode—Same Tree

    1.题目描述 Given two binary trees, write a function to check if they are equal or not.   Two binary tree ...

  10. sqlserver任务导出Excle

    --sql语句就用下面的存储过程 /*--数据导出Excel 导出查询中的数据到Excel,包含字段名,文件为真正的Excel文件,如果文件不存在,将自动创建文件,如果表不存在,将自动创建表基于通用性 ...