判断数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   |  [] instanceof Array;
 
  Object.prototype.toString.call([]) === '[object Array]'
 
  Array.prototype.isPrototypeOf([]);
 
  [].constructor === Array;
 
  Array.isArray([]);
 
  | 
 
如何处理类数组对象
JavaScript类数组对象的定义
可以同过索引访问元素, 并且拥有length属性
没有数组的方法,例如 push, forEach, indexOf等
1 2 3 4 5 6
   | var foo = {   0: 'js',   1: 'Node',   2: 'TS',   length: 3 };
  | 
 
转换方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   |  Array.prototype.slice.call(arguments); Array.prototype.slice.apply(arguments); [].slice.call(arguments)
 
  [...arguments]
 
  Array.from(arguemnts);
 
  [].concat.apply([], arguments)
 
  function toArray(s) {   var arr = [];   for(var i =0, len =s.length; i< len; i++>) {     arr[i] = s[i]   }   return arr; }
 
  | 
 
注意
- 数组的长度由类数组的length决定
 
- 索引不连续,会自动补位undefined
 
- 仅仅考虑0和正整数索引
 
- slice会产生系数数组,内容是empty而不是undefined
 
- 类数组push注意,push操作是length所在的位置