how does Array.prototype.slice.call() work?
763
down vote
accepted
+50
What happens under the hood is that when .slice() is called normally, this is an Array, and then it just iterates over that Array, and does its work.
How is this in the .slice() function an Array? Because when you do:
object.method();
...the object automatically becomes the value of this in the method(). So with:
[1,2,3].slice()
...the [1,2,3] Array is set as the value of this in .slice().
But what if you could substitute something else as the this value? As long as whatever you substitute has a numeric .length property, and a bunch of properties that are numeric indices, it should work. This type of object is often called an array-like object.
The .call() and .apply() methods let you manually set the value of this in a function. So if we set the value of this in .slice() to an array-like object, .slice() will just assume it's working with an Array, and will do its thing.
Take this plain object as an example.
var my_object = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
length: 5
};
This is obviously not an Array, but if you can set it as the this value of .slice(), then it will just work, because it looks enough like an Array for .slice() to work properly.
var sliced = Array.prototype.slice.call( my_object, 3 );
Example: http://jsfiddle.net/wSvkv/
As you can see in the console, the result is what we expect:
['three','four'];
So this is what happens when you set an arguments object as the this value of .slice(). Because arguments has a .length property and a bunch of numeric indices, .slice() just goes about its work as if it were working on a real Array.
https://stackoverflow.com/questions/7056925/how-does-array-prototype-slice-call-work
how does Array.prototype.slice.call() work?的更多相关文章
- 【javascript 技巧】Array.prototype.slice的妙用
Array.prototype.slice的妙用 开门见山,关于Array 的slice的用法可以参考这里 http://www.w3school.com.cn/js/jsref_slice_arra ...
- Array.prototype.slice.call(arguments)
Array.prototype.slice.call(arguments)能够将具有length属性的对象转化为数组, 可以理解为将arguments转化成一个数组对象,让它具有slice方法 如: ...
- IE下Array.prototype.slice.call(params,0)
i8 不支持 Array.prototype.slice.call(params,0) params可以是 HTMLCollection.类数组.string字符串
- (转)Array.prototype.slice.call自解
很多框架或者库里面都会有这句的使用,最多的还是通过Array.prototype.slice.call(arguments,0)把arguments这个伪数组转换为真正的数组.但为什么可以这么做,却一 ...
- 详解 Array.prototype.slice.call(arguments)
首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组 在这里我们看第二个方法 1.在JS里Array是一个类 slice是 ...
- Array.prototype.slice && Array.prototype.splice 用法阐述
目的 对于这两个数组操作接口,由于不理解, 往往被误用, 或者不知道如何使用.本文尝试给出容易理解的阐述. 数组 什么是数组? 数组是一个基本的数据结构, 是一个在内存中依照线性方式组织元素的方式, ...
- Array.prototype.slice.call(document.querySelectorAll('a'), 0)
Array.prototype.slice.call(document.querySelectorAll('a'), 0)的作用就是将一个DOM NodeList 转换成一个数组. slice()方法 ...
- Array.prototype.slice.call
Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 ,::'age'}; Array.prototype.slice.call(arr); ...
- Array.prototype.slice.call(arguments) 类数组转成真正的数组
Array.prototype.slice.call(arguments) 我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...
- JavaScript 兼容 Array.prototype.slice.call
IE9之前的IE版本的HTMLCollection以及NodeList不是Object的子类. 在通过Array.prototype.slice.call进行数组化的时候,IE9之前的IE版本会抛出异 ...
随机推荐
- 亚马逊免费服务器搭建Discuz!论坛过程(三)
参考文章:http://faq.comsenz.com/library/system/env/env_linux.htm 参考但是不要完全按照上述文章,不然很可能出错. 尤其不要init 6 重启,我 ...
- js获取数组中任意一项
1.获取数组任一项 在一些实际应用场景中,会要求实现一个随机的需求,随机获取某一项,来展示出来,都知道要通过javaScript的Math.random()方法来实现,这里我在Array的原型上添加了 ...
- WordPress TinyMCE 编辑器增强技巧大全
说到WordPress自带的TinyMCE 编辑器,有些国人总是不太满意.针对这个情况,倡萌已经介绍了一些增强或替代的方法: WordPress编辑器增强插件:TinyMCE Advanced Wor ...
- poj 3469 最小割模板sap+gap+弧优化
/*以核心1为源点,以核心2为汇点建图,跑一遍最大流*/ #include<stdio.h> #include<string.h> #include<queue> ...
- hdu 2586 lca在线算法(朴素算法)
#include<stdio.h> #include<string.h>//用c/c++会爆栈,用g++ac #define inf 0x3fffffff #define N ...
- 0320SQL中的where条件,在数据库中提取与应用浅析
转自 何登成的技术博客 追求技术的道路上,10年如一日 首页 关于我 RSS 订阅 © 2012-2017 何登成的技术博客 SQL中的where条件,在数据库中提取与应用浅析 3月 3r ...
- rabbit-入门
#启用rabbit的web管理 rabbitmq-plugins.bat enable rabbitmq_management 发布的窗口用TOPIC模式 rabbitmq没有确认消息接收的返回值,M ...
- 洛谷—— P2647 最大收益
https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,……,n.你可以在这当中任意选择任意多个物品.其中第i个物 ...
- arm32位固定指令中怎么容纳32位变量
在ARM指令集汇编码中.32位有效马上数是通过______偶数位而间接得到的 A.循环左移 B.循环右移. C.逻辑左移. D.逻辑右移 答案为循环左移.为什么?还有最好解释一下逻辑移动和循环移动的概 ...
- 安装10gR2的硬件要求
1.至少1G的RAM. 2.RAM与swap关系: RAM swap 512M以上 2*RAM (非常奇怪.至少1G的RAM.还写512的 ...