码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-09 13:15:26      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:log   color   OLE   pre   arguments   code   取数   var   调用   

Array.prototype.slice.call(arguments,num) 能将具有length属性的对象转成数组。
 
slice 从字面上的意思可以理解为截取数组的一部分。
call 从字面上的意思可以理解为截取出来
arguments 仅与数组类似,不是真正的数组对象,所以它并没有slice这个方法,让arguments转换成一个数组对象,让arguments具有slice()方法。
注:要是直接写arguments.slice(num)会报错。
那么 Array.prototype.slice.call(arguments,num); 即把调用方法的参数截取出来。 
如:
var a={length:2,0:‘first‘,1:‘second‘};//类数组,有length属性,长度为2,第0个是first,第1个是second
console.log(Array.prototype.slice.call(a,0));// ["first", "second"],调用数组的slice(0);

var a={length:2,0:‘first‘,1:‘second‘};
console.log(Array.prototype.slice.call(a,1));//["second"],调用数组的slice(1);

var a={0:‘first‘,1:‘second‘};//去掉length属性,返回一个空数组
console.log(Array.prototype.slice.call(a,0));//[]
function test(){
  console.log(Array.prototype.slice.call(arguments,0));//["a", "b", "c"],slice(0)
  console.log(Array.prototype.slice.call(arguments,1));//["b", "c"],slice(1)
}
test("a","b","c");
 

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

标签:log   color   OLE   pre   arguments   code   取数   var   调用   

原文地址:https://www.cnblogs.com/renzm0318/p/8747446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!