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

在很多时候经常看到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;
    }
}
时间: 2024-08-27 02:12:44

理解Array.prototype.slice.call(arguments)的相关文章

伪数组转为数组 Array.prototype.slice.call(arguments)

我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换) 1 var a={length:2,0:'first',1:'second'}; 2 Array.prototype.slice.call(a);// ["first", "second"] 3 4 var a={length:2}; 5

浅谈Array.prototype.slice.call(arguments,0)

在谈论这个问题之前 我们先了解下call的用法及作用 对象.方法.call(obj[,arg1[, arg2[,   [,.argN]]]]]):调用一个对象的一个方法,以另一个对象替换当前对象. 1 function Personal(a){ 2 3 this.name = a; 4 5 this.say = function(){ 6 7 alert(a) 8 } 9 10 } 11 12 function Man(a){ 13 14 this.sex = "maie"; 15 P

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

我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换) 如: 1 var a={length:2,0:'first',1:'second'}; 2 Array.prototype.slice.call(a);// ["first", "second"] 3 4 var a={length:2}

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

我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换) 如: 1 var a={length:2,0:'first',1:'second'}; 2 Array.prototype.slice.call(a);// ["first", "second"] 3 4 var a={length:2}

将函数的实际参数转换成数组的方法,习惯用Array.prototype.slice.call(arguments)

实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组. 我们可以通过数组的 slice 方法将 arguments 对象转换成真正的数组. 方法一:通过Array.prototype属性调用slice方法 var args = Array.prototype.slice.call(arguments); Array 本身是没有 slice 方法,它的方法在 Arr

Array.prototype.slice.call(arguments)

话说可以通过call()来让arguments来继承array的slice()方法. 可是为何使用arguments.slice(1)会报错, 而这样使用Array.prototype.slice.call(arguments,1)就可以正常. function name(){ Array.prototype.slice.call(arguments); console.log(arguments.slice(1)); } name("some","thing");

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, b); console.log(a) //[1,2,3,4,5,6] 其实这样的写法等价于: var a = [1,2,3]; var b = [4,5,6]; Array.prototype.push.apply(a, b); console.log(a) //[1,2,3,4,5,6] 这样

理解 Array.prototype.slice.apply

1. 首先,我们都理解在js中改变this引用有三种方法,call(), apply(), bind(): 2. bind方法是改变函数内this引用,简单不再描述; 3. 至于 call() 和 apply() 两个方法,区别在于,前者是不定长的入参,后者是一个数组:下边重点说apply方法使用; 原理: 我们知道js中存在一种类数组对象,比如 {0:1,length:1}    或者 DOM 对象,或者 arguments 对象: 数组只是一种特殊的对象,数组的特殊性体现在,它的键默认是按次

【笔记】js Array.prototype.slice.call(arguments) 将函数的参数转换为数组方法的见解

我们知道函数里面的参数实际上是一个以数组形式储存的对象 但它并非一个数组 如果我们要将它转换为数组可以调用Array.prototype.slice() 这个方法 分析一下这个方法: Array.prototype:Array其实一个类名,但是调用类里面的方法只能够通过类的实例对象调用所以这里用了  Array.prototype 通过它自身的原型对象调用 其次是slice():这个是Array类里面的一个方法功能是截取数组里面的某一部分内容,它接收两个参数slice('数组下标起始位置','数