JavaScript中的Array.prototype.slice.call(arguments)能将有length属性的对象转换为数组(特别注意: 这个对象一定要有length属性). 但有一个例外,IE下的节点集合它不能转换(因为IE下的dom对象是以com对象的形式实现,js对象和com对象不能进行转换)
首先,我们来看看JavaScript中的slice用法, 在JavaScript中Array是一个类,slice是此类中的一个方法,slice的中文意思是 ‘截取’
一个是String.slice => 返回值是字符串
一个是Array.slice => 返回值是数组
Array.prototype.slice.call(arguments)能够将具有length属性的arguments转换为数组, 我们可以理解为就是 arguments.toArray().slice()
所以,这个过程我们是不是可以理解为 Array.prototype.slice.call(arguments)的实现过程就是把传入进来的具有length属性的第一个参数arguments转换为数组,再调用它的slice(截取)方法
这个Array.prototype.slice.call(arguments) 不单有slice方法,还有call方法。 那么call方法又是如何用的呢
我们来看一个用call方法的例子:
var a = function(){
console.log(this);
console.log(typeof this);
console.log(this instanceof String);
}
a.call('littleLuke');
输出如下
// 'littleLuke'
// Object
// true
以下是上面的代码片段在Visual Studio Code中的执行
从上面的代码片段中,我们可以看到调用了函数a的call方法之后,我们可以看到把call方法中的参数传入到函数a的作用域中去了,也就是说函数a中的this此时指向的就是它调用的call方法中的参数
我们上面说了,Array.Prototype.slice.call()除了call方法外,还有slice方法,那么JavaScript中Array的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;
}
所以整个过程我们基本就可以分2步进行理解了
Array.prototype.slice.call(arguments)
理解第一步: 其中,arguments是一个具有length属性的对象, 通过call 这个方法,把arguments 指向了Array.prototype.slice方法的作用域,也就是说通过call方法,让Array.prototype.slice对arguments对象进行操作
理解第二步: Array.prototype.slice就是对该对象使用Array类的slice方法。但是呢arguments它又不是个Array对象
typeof arguments === "Object" //不是"Array"
所以我们没法用arguments.slice()方法,这样是会报错的。 所以这里,我们需要使用Array.prototype.slice, 它的作用就是第一步把arguments转换为一个Array对象,第二步对转换后的Array对象使用slice方法
理解了整个过程后,我们来看一些具体的例子,
下面是我自己在Visual Studio Code中执行的截图
在文章的结尾,来说两个在实际开发中比较常用到的通用方法
1. 将函数的实际参数转换成数组的方法 (3个方法)
方法一 : var args = Array.prototype.slice.call(arguments);
方法二: var args = [].slice.call(arguments);
方法三:
2. 转换成数组的通用函数
在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理:
1、基本讲解
-
1.在JS里Array是一个类 slice是此类里的一个方法 ,那么使用此方法应该Array.prototype.slice这么去用
slice从字面上的意思很容易理解就是截取(当然你不是英肓的话) 这方法如何使用呢?arrayObj.slice(start, [end])
很显然是截取数组的一部分。 -
2.我们再看call
call([thisObj[,arg1[arg2[[argN]]]]])
thisObj是一个对象的方法
arrg1~argN是参数
那么Array.prototype.slice.call(arguments,1);
这句话的意思就是说把调用方法的参数截取出来。
如:
function test(a,b,c,d)
{
var arg = Array.prototype.slice.call(arguments,1);
alert(arg);
}
test("a","b","c","d"); //b,c,d
2、疑惑解答
先给个例子,这是jqFloat插件里的代码:
if (element.data('jDefined')) {
if (options && typeof options === 'object') {
methods.update.apply(this, Array.prototype.slice.call(arguments, 1));
}
} else {
methods.init.apply(this, Array.prototype.slice.call(arguments, 1));
}
多次用到 Array.prototype.slice.call(arguments, 1),不就是等于 arguments.slice(1) 吗?像前者那样写具体的好处是什么?这个很多js新手最疑惑的地方。那为什么呢?
因为arguments并不是真正的数组对象,只是与数组类似而已,所以它并没有slice这个方法,而Array.prototype.slice.call(arguments, 1)
可以理解成是让arguments转换成一个数组对象,让arguments具有slice()方法。要是直接写arguments.slice(1)会报错。
typeof arguments==="Object" //而不是 "Array"
3、真正原理
Array.prototype.slice.call(arguments)
能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换)
如:
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");
补充:
将函数的实际参数转换成数组的方法
方法一:var args = Array.prototype.slice.call(arguments);
方法二:var args = [].slice.call(arguments, 0);
方法三:
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
最后,附个转成数组的通用函数
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]; //据说这样比push快
}
return arr;
}
}