Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组

首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组

假设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]);
       //result[i] = this[i]; //据说性能更高
}
return result;
}

以上代码如何理解,首先Array.propotype.slice()方法是将数组进行循环赋给新建的数组,然后return返回,start、end参数默认参数是0和this对象的长度,传参则取传参值;

使用Array.protortype原型作为对象时需要配合传第一个参数arguments来改变this指向,Array.prototype.slice.call(arguments),start、end参数默认;

最后return的数组即是我们需要转成的数组。

Array.prototype.slice.call(arguments)探究的更多相关文章

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

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

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

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

  3. javascript:Array.prototype.slice.call(arguments)

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

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

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

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

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

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

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

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

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

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

  9. [转] 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)

    假如你是一个 JavaScript 开发者,你可能见到过 Array.prototype.slice.apply(arguments) 这样的用法,然后你会问,这么写是什么意思呢? 这个语法其实不难理 ...

随机推荐

  1. linux机器之间拷贝和同步文件命令

    1 不同机器拷贝文件 scp 文件     登录用户@机器IP:/目录/子目录 scp filename test@10.20.130.202:/home/test/ 2 文件[夹]同步 rsync ...

  2. 雷林鹏分享:Ruby 安装 - Windows

    Ruby 安装 - Windows 下面列出了在 Windows 机器上安装 Ruby 的步骤. 注意:在安装时,您可能有不同的可用版本. 下载最新版的 Ruby 压缩文件.请点击这里下载. 下载 R ...

  3. python 在 Windows Server 2008 r2 上 安装失败

    Microsoft Visual C++ 2008 Redistributable Package link (x86): https://www.microsoft.com/en-us/downlo ...

  4. LeetCode--021--合并两个有序链表

    问题描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1- ...

  5. 20170714xlVba多个工作簿转多个Word文档表格

    Public Sub SameFolderGather() Application.ScreenUpdating = False Application.DisplayAlerts = False A ...

  6. js-Client-side web APIs

    APIs https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/ 简介: 应用程序接口(API) ...

  7. Nanami's Digital Board CodeForces - 434B (棋盘dp)

    大意: 给定01矩阵, m个操作, 操作1翻转一个点, 操作2求边界包含给定点的最大全1子矩阵 暴力枚举矩形高度, 双指针统计答案 #include <iostream> #include ...

  8. Java数组常用API

    java.util.Arrays Arrays.asList() 数组转换成列表 String[] strArray = {"zhang", "xue", &q ...

  9. Oracle性能诊断艺术-读书笔记

    create table test0605 as select * from dba_objects; select t1.owner,t1.object_name,t1.object_id from ...

  10. HDOJ1001

    #include<iostream> using namespace std; int main() { long long n; while(cin >> n) { cout ...