关于数组的长度相关如下:
类似数组的对象,其实就是它的key刚好是数值且有length属性,如arguments
数组的slice
方法可以将“类似数组的对象”变成真正的数组。
var arr = Array.prototype.slice.call(arrayLike);
除了转为真正的数组,“类似数组的对象”还有一个办法可以使用数组的方法,就是通过call()
把数组的方法放到对象上面。
function print(value, index) {
console.log(index + ' : ' + value);
}
Array.prototype.forEach.call(arrayLike, print);
小记:apply和call相当于把已有的方法嫁接到某个对象上(原来没有该方法),使得该对象拥有该方法,两种的区别在于传参不同。