转对象(含length属性)成数组Array.prototype.slice.call(arguments)
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个。

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');


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)的更多相关文章
- Array.prototype.slice.call(arguments) 类数组转成真正的数组
		
Array.prototype.slice.call(arguments) 我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...
 - JS之函数实际参数转换成数组的方法[].slice.call(arguments)
		
实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组. 我们可以通 ...
 - 理解Array.prototype.slice.call(arguments)
		
在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理: 1.基本讲解 1.在 ...
 - javascript:Array.prototype.slice.call(arguments)
		
我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...
 - Array.prototype.slice.call(arguments)探究
		
Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 首先,slice有两个用法,一个是String.slice,一个是Array.slic ...
 - 解析Array.prototype.slice.call(arguments)
		
在es5标准中,我们经常需要把arguments对象转换成真正的数组 // 你可以这样写 var arr = Array.prototype.slice.call(arguments) // 你还可以 ...
 - 解析 Array.prototype.slice.call(arguments,0)
		
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
 - Array.prototype.slice.call(arguments) 通俗法理解
		
Array.prototype.slice.call(arguments,num) 能将具有length属性的对象转成数组. slice 从字面上的意思可以理解为截取数组的一部分. call 从字 ...
 - js Array.prototype.slice.call(arguments,0) 理解
		
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
 
随机推荐
- 关于数据结构的10个面试题(c语言实现)
			
关于数据结构的10个面试题(c语言实现) 2010-04-21 22:17 5702人阅读 评论(0) 收藏 举报 数据结构面试c语言bttree 1. 输入一个链表的头结点,从尾到头 ...
 - jenkins 进阶系列网址
			
http://www.cnblogs.com/zz0412/tag/jenkins/default.html?page=1
 - jar2exe 配置jre
			
http://www.regexlab.com/zh/jar2exe/support.htm
 - 转 mybatis javaType与jdbcType对应
			
java.sql.Types 值 Java 类型 IBM DB2 Oracle Sybase SQL Informix IBM Content Manager BIGINT java.lang.l ...
 - git使用系列(一)
			
git commit 的时候出现了问题: change not staged for commit. no changes added to commit(use "git add" ...
 - CSS-学习笔记六
			
1. 自适应,响应式布局 2. pure 3. Animate
 - kafka 集群部署 多机多broker模式
			
kafka 集群部署 多机多broker模式 环境IP : 172.16.1.35 zookeeper kafka 172.16.1.36 zookeeper kafka 172.16 ...
 - tp框架实现文件上传
			
public function shangchuan() { $this->display(); } public function upload() { $uplode= new \Think ...
 - Redis数据备份和重启恢复
			
一.对Redis持久化的探讨与理解 目前Redis持久化的方式有两种: RDB 和 AOF 首先,我们应该明确持久化的数据有什么用,答案是用于重启后的数据恢复. Redis是一个内存数据库,无论是RD ...
 - JS表单原生验证器
			
一.前言 最近在开发一个新项目,需要做登陆等一系列的表单提交页面.在经过“缜密”的讨论后,我们决定 不用外部流行的框架,如bootstrap,由于我负责的模块 仅仅是其中的一部分,因此少数服从多数,无 ...