解析 Array.prototype.slice.call(arguments,0)
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数
网上很多复制粘帖说:Array.prototype.slice.call(arguments)能将具有length属性的对象 转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换)
关键点:
1、Array是构造函数
2、arguments是类数组对象(缺少很多数组的方法)
3、call让一个对象调用另一个对象的方法。你可以使用call()来实现继承:写一个方法,然后让另外一个新的对象来继承它(而不是在新对象中再写一次这个方法)
4、 slice从一个数组中切割,返回新的数组,不修改切割的数组
so,其实本质就是arguments这个对象使用了数组的slice这个方法,得到了参数构成的数组(也可以用apply)。
Array.prototype.slice.call(arguments, [0, arguments.length])
//使用prototype只是因为Array是构造函数
Array.prototype.slice.call([1,2,3,4,5],0)// [1, 2, 3, 4, 5]
[].slice.call([1,2,3,4,5],1)// [2, 3, 4, 5]
//没有length的对象
var a={length:2, 0:'first', 1:'second'};
Array.prototype.slice.call(a);// ["first", "second"]
var a={length:2, 0:'first', 1:'second'};
Array.prototype.slice.call(a,1);// ["second"]
var a={0:'first', 1:'second'};
Array.prototype.slice.call(a,1);// []
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]; //console.timeEnd测试以后比push快
}
return arr;
}
}
解析 Array.prototype.slice.call(arguments,0)的更多相关文章
- js Array.prototype.slice.call(arguments,0) 理解
Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...
- 解析Array.prototype.slice.call(arguments)
在es5标准中,我们经常需要把arguments对象转换成真正的数组 // 你可以这样写 var arr = Array.prototype.slice.call(arguments) // 你还可以 ...
- 理解Array.prototype.slice.call(arguments)
在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理: 1.基本讲解 1.在 ...
- Array.prototype.slice.call(arguments) 通俗法理解
Array.prototype.slice.call(arguments,num) 能将具有length属性的对象转成数组. slice 从字面上的意思可以理解为截取数组的一部分. call 从字 ...
- 观V8源码中的array.js,解析 Array.prototype.slice为什么能将类数组对象转为真正的数组?
在官方的解释中,如[mdn] The slice() method returns a shallow copy of a portion of an array into a new array o ...
- 详解 Array.prototype.slice.call(arguments)
首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组 在这里我们看第二个方法 1.在JS里Array是一个类 slice是 ...
- Array.prototype.slice.call(arguments) 类数组转成真正的数组
Array.prototype.slice.call(arguments) 我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...
- 转对象(含length属性)成数组Array.prototype.slice.call(arguments)
我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...
- js基础进阶--关于Array.prototype.slice.call(arguments) 的思考
欢迎访问我的个人博客:http://www.xiaolongwu.cn Array.prototype.slice.call(arguments)的作用为:强制转化arguments为数组格式,一般出 ...
随机推荐
- The Road to learn React书籍学习笔记(第四章)
高级React组件 本章将重点介绍高级 React 组件的实现.我们将了解什么是高阶组件以及如何实现它们.此外,我们还将深入探讨 React 中更高级的主题,并用它实现复杂的交互功能. 引用 DOM ...
- python2.7练习小例子(十六)
16):题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. 程序分析:利用 while 或 for 语句,条件为输入的字符不为 '\n'. 程序源代码: #!/u ...
- Oracle错误记录
1 SQLPlus无法登陆oracle,PLSql可以登陆,报错ORA-12560 环境变量 右击计算机属性-->高级系统设置-->高级-->环境变量-->系统变量--> ...
- LeetCode:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...
- Messy Code in Windows Server 2008 R2 English Edition
We always use Windows Server 2008 R2 English operation system. And it doesn't have any problem ...
- Struts2(七.删除和修改用户功能的实现)
一.删除功能 之前跳转用户列表的时候把用户id,用户名,用户密码存入了ActionContext 1.userlist.jsp <%@ page language="java" ...
- 剑指offer-树的子结构17
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) class Solution: def issubTree(self,pRoot1,pRoot2) ...
- React错误总结(三)
神坑react native之Cannot Add a child that doesn't have a YogaNode to a parent with out a measure functi ...
- POJ 1144 Network(割点)
Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are c ...
- lintcode-118-不同的子序列
118-不同的子序列 给出字符串S和字符串T,计算S的不同的子序列中T出现的个数. 子序列字符串是原始字符串通过删除一些(或零个)产生的一个新的字符串,并且对剩下的字符的相对位置没有影响.(比如,&q ...