Array.prototype.slice.call(arguments)

作用:能够把类数组转化成真正的数组,什么是类数组(有length属性,属性值为数字;其他属性值为数字‘0’,‘1’,等


原理:看一下slice的源码

  1. function slice(start, end) {   
  2. var len = ToUint32(this.length), result = [];   
  3. for(var i = start; i < end; i++) {   
  4.     result.push(this[i]);   
  5. }   
  6.     return result;   
  7. }  

slice 并不需要 this 为 array 类型,只需要有 length 属性即可。并且 length 属性可以不为 number 类型,当不能转换为数值时,ToUnit32(this.length) 返回 0. 

猜你喜欢

转载自blog.csdn.net/qq_37016928/article/details/80377712
今日推荐