判斷數據是否類數組

檢測函數(臨時記錄)

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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章