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

  1. js Array.prototype.slice.call(arguments,0) 理解

    Array.prototype.slice.call(arguments,0) 经常会看到这段代码用来处理函数的参数 网上很多复制粘帖说:Array.prototype.slice.call(argu ...

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

    在es5标准中,我们经常需要把arguments对象转换成真正的数组 // 你可以这样写 var arr = Array.prototype.slice.call(arguments) // 你还可以 ...

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

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

  4. Array.prototype.slice.call(arguments) 通俗法理解

    Array.prototype.slice.call(arguments,num) 能将具有length属性的对象转成数组.   slice 从字面上的意思可以理解为截取数组的一部分. call 从字 ...

  5. 观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 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 修改mysql root密码的方法

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  2. Android开发——Google关于Application Not Responding的建议

    秒内没有执行完毕. 2.       避免ANR的一些建议 Android applications normally run entirely on asingle (i.e. main) thre ...

  3. Service ANR问题

    错误堆栈: ActivityManager: ANR in com.oppo.reader PID: 8071 Reason: executing service com.oppo.reade//co ...

  4. FJOI 2019 游记

    (FJOI 2019 滚粗记) Day 0 早上刷了一些水题,然后就上路了. (画图3D好好玩) 来得晚只有十几分钟时间看考场,于是连试机题都没有Ak. Day 1 果然我还是太菜了 走过来的时候再讨 ...

  5. Qt 隐藏标题栏可移动升级版

    在最出的时候,在Qt程序隐藏标题栏的情况下,实现界面可拖拽移动,是鼠标在在程序界面的任意位置都可以,现在这个版本是需要鼠标在程序界面的特定位置开可以 上代码 static QPoint last(0, ...

  6. 在Kotlin上怎样用Mockito2 mock final 类(KAD 23)

    作者:Antonio Leiva 时间:Mar 2, 2017 原文链接:https://antonioleiva.com/mockito-2-kotlin/ 如我们在前面文章中谈到的,Kotlin最 ...

  7. UVA 1085 House of Cards(对抗搜索)

    Description   Axel and Birgit like to play a card game in which they build a house of cards, gaining ...

  8. matlab mex 小o -o 出错

    https://github.com/kyamagu/mexopencv/issues/117 就是说2014a以后的版本mex   -o 选项变成了 -output 蛋疼,这有什么好改的.找了好久才 ...

  9. Jquery tmpl详解

    Jquery tmpl是动态请求数据来更新页面非常常用的方法,比如博客评论的分页动态加载,微博的滚动加载和定时请求加载等. 这些情况下,动态请求返回的数据一般不是已拼好的 HTML 就是 JSON 或 ...

  10. Activiti工作流(三)——流程变量

    流程变量可以是流程中一系列参数,比如办理人(Assignee),消息(message)等.这些流程变量使得activiti能够应用于更为复杂的业务中,使得流程变得更加灵活可控. 场景(一) 图一:没有 ...