JavaScript 类数组转化为数组
很多情况下我们需要将类数组的对象(key是以0到n的数字或字符串,具有length属性。例如:Arguments对象)转化为一个数组来进行各种例如forEach的数组操作,在ES5中是利用类数组对象强制调用Array对象的slice方法来进行转换的,在ES6中Array扩展了from方法来进行转换,另外,ES6中的扩展运算符也可将某些类数组对象转化为数组各方法示例如下: 强制调用Array对象的slice方法 console.log(Array.prototype.slice.call({ '0': 'a', '1': 'b', '2': 'c', length: 3 })); //Array(3) [ "a", "b", "c" ] Array.from()方法 console.log(Array.from({ '0':'a', '1':'b', '2':'c', length:3 })); //Array(3) [ "a", "b", "c" ] 扩展运算符... console.log((function (a,b,c) { console.log([...arguments])...
