判断数据是否类数组

检测函数(临时记录)

function isArrayLikeObj(obj) {
  // For document.all
  if (getTypeString(obj) === 'HTMLAllCollection') {
    return true;
  }
  if (obj === null || typeof obj !== 'object') {
    return false;
  }
  if (Object.prototype.hasOwnProperty.call(obj, 'length') === false) {
    return false;
  }
  if (typeof obj.length !== 'number') {
    return false;
  }
  if (Number.isFinite(obj.length) === false) {
    return false;
  }
  if (typeof obj[Symbol.iterator] !== 'function') {
    return false;
  }
  if (obj.constructor === Array) {
    return false;
  }
  const LIMIT_LEN_VAL = Math.pow(2, 53) - 1;
  return obj.length >= 0 && obj.length < LIMIT_LEN_VAL;
}

 

posted @ 2022-09-29 23:58  樊顺  阅读(21)  评论(0编辑  收藏  举报