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?的更多相关文章

  1. 【javascript 技巧】Array.prototype.slice的妙用

    Array.prototype.slice的妙用 开门见山,关于Array 的slice的用法可以参考这里 http://www.w3school.com.cn/js/jsref_slice_arra ...

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

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

  3. IE下Array.prototype.slice.call(params,0)

    i8 不支持 Array.prototype.slice.call(params,0) params可以是 HTMLCollection.类数组.string字符串

  4. (转)Array.prototype.slice.call自解

    很多框架或者库里面都会有这句的使用,最多的还是通过Array.prototype.slice.call(arguments,0)把arguments这个伪数组转换为真正的数组.但为什么可以这么做,却一 ...

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

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

  6. Array.prototype.slice && Array.prototype.splice 用法阐述

    目的 对于这两个数组操作接口,由于不理解, 往往被误用, 或者不知道如何使用.本文尝试给出容易理解的阐述. 数组 什么是数组? 数组是一个基本的数据结构, 是一个在内存中依照线性方式组织元素的方式, ...

  7. Array.prototype.slice.call(document.querySelectorAll('a'), 0)

    Array.prototype.slice.call(document.querySelectorAll('a'), 0)的作用就是将一个DOM NodeList 转换成一个数组. slice()方法 ...

  8. Array.prototype.slice.call

    Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 ,::'age'}; Array.prototype.slice.call(arr); ...

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

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

  10. JavaScript 兼容 Array.prototype.slice.call

    IE9之前的IE版本的HTMLCollection以及NodeList不是Object的子类. 在通过Array.prototype.slice.call进行数组化的时候,IE9之前的IE版本会抛出异 ...

随机推荐

  1. C#中为什么字段设为只读依然可以在构造函数中为它赋值

    因为只读是为了保证在类的 实例 被 创建后 ,当前属性不能被改变 构造函数中实例还没创建完成,所以依然可以改变

  2. eas之编码规则&单据转换规则

    *当在企业建模中没有要显示的项目的话,则从包更新到系统树然后选择到规则定义,对申请单新增规则. 企业建模--业务规则-规则定义组织优先  多组织有先  集团优先固定值 显示格式PUR ..系统日期 2 ...

  3. Java语言输出HelloWorld

    1.新建一个名为HelloWorld.txt的文本文件. 2.将HelloWorld.txt改为.java. 3.将HelloWorld.java用文本文件的方式打开,并输入代码. 4.打开命令行,进 ...

  4. 洛谷P1060 开心的金明【dp】

    金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行&qu ...

  5. linux下关闭键盘的重复击键

    https://askubuntu.com/questions/576421/disable-keyboard-repeat-from-command-line You can use the xse ...

  6. C# DataGridView 使用

    之前咩有做个界面的东西,更没有使用过DataGirdView 这个控件. 现在本来是准备用DeV呢,结果发现我的DEV没有注册,只好暂时用这个DataGridView来替代使用了. 我现在要是设置两列 ...

  7. orcale 单行函数之数字函数, 日期函数

    日期函数: 案例:

  8. java反射意义

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...

  9. Java内联函数

    1.内联函数就是指函数在被调用的地方直接展开,编译器在调用时不用像一般函数那样,參数压栈,返回时參数出栈以及资源释放等,这样提高了程序运行速度. 2.Java语言中有一个keywordfinal来指明 ...

  10. luogu2618 数字工程 DP

    题目大意:ACM实验室开启了一个数字工程项目,希望把正整数n通过一些特殊方法变成1.可采用的方法有:(1)减去1:(2)除以它的任意一个素因子. 每操作一次消耗一个单位的能量.问,把n变成1最少需要消 ...