# JavaScript中的对象转数组Array.prototype.slice.call()方法详解

JavaScript中的对象转数组Array.prototype.slice.call()方法详解

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(截取)方法

然后,再看call()方法

这个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

从上面的代码片段中,我们可以看到调用了函数a的call方法之后,我们可以看到把call方法中的参数传入到函数a的作用域中去了,也就是说函数a中的this此时指向的就是它调用的call方法中的参数

slice方法内部实现源码

我们上面说了,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;
}

结合起来看 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方法

理解了整个过程后,我们来看一些具体的例子,

let a={length:3,0:'zero',1:'one',2:'two'}
let b={length:3,0:'zero',1:'one',5:'five'} //对象前属性名必须按顺序,含length属性
let c=['zero','one','two','three']
console.log(Array.prototype.slice.call(a,0)) //第二个参数0表示start即 slice(start,end)
console.log(Array.prototype.slice.call(b)) //如没第二参数,则取全部
console.log(Array.prototype.slice.call(c,3)) //取下标为3开始到结束

/*结果:
[ 'zero', 'one', 'two' ]
[ 'zero', 'one', <1 empty item> ]
[ 'three' ]
*/

下面是我自己在Visual Studio Code中执行的截图

img

实际开发中比较常用到的通用对象转数组方法

1.将函数的实际参数转换成数组的方法 (3个方法)

方法一 : var args = Array.prototype.slice.call(arguments);

方法二: var args = [].slice.call(arguments);

方法三:

function getArray(arguments){
	var args=[];
    for(let i=0; i<arguments.length; i++){
        args.push(arguments[i]);
    }
    return args;
}

2. 转换成数组的通用函数

let toArray = function(s){
    try{
        Array.prototype.slice.call(s);
    }catch(e){
        var arrResult=[];
        for(let i=0,len=s.length; i<len; i++){
            //arrResult.push(s[i]); do this is fine
            arrResult[i]=s[i]; //this operation is quicker than above(上面) one
        }
        return arrResult;
    }
}

img

posted @ 2020-12-06 20:33  晨光曦微  阅读(320)  评论(0编辑  收藏  举报