javascript - 是 'isArraylike' 吗?

标签 javascript arrays

至于我的理解,JavaScript 中的“ArrayLike”类型是一个具有自己 数字正整数键的对象,在连续顺序(稀疏数组等)中不是必需的,其中对象的 (int) .length 属性更大,对吧?我遇到了很多关于这件事的解释,但每一个都给出了不同的观点,所以我编写了查找 max (int) key 的函数,丢弃 .length 属性(以防止 Math.max() 产生 NaN 值,将 false 与任何值进行比较),而对象自身的其余键保持不变。如果它们都是数字,它返回最大一个,如果不是,它返回 NaN(返回 false 作为结果),并将它的输出与接受的 jQuery 等效函数的结果进行比较值。欢迎所有解释。这是我运行的代码:

// /jQuery version
function isArraylike(obj) {

  // casted the obj parameter to Object here 
  // because some primitive values was throwing an error 
  // not supporting .property access or 'in' operand
  // it doesn't change the logic just converts them to equivalent wrapped types

  var length = (obj = Object(obj)).length,
    type = jQuery.type(obj);

  if (type === "function" || jQuery.isWindow(obj)) {
    return false;
  }

  if (obj.nodeType === 1 && length) {
    return true;
  }

  return type === "array" || length === 0 ||
    typeof length === "number" && length > 0 && (length - 1) in obj;
}

// and my version
function isalike(o) {

  try {

    o.length;

    return (Math.max.apply(Math,
      erase(Object.keys(o), "length")
    ) < o.length);

  } catch (e) {}

  return false;

  function erase(arr, val) {
    var idx;
    ((idx = arr.indexOf(val)) != -1) && arr.splice(idx, 1);
    return arr;
  }

  function s(arg) {
    return Array.prototype.slice.call(arg, 0);
  }

}

// and tests cases that came to my mind
// first boolean value in side comment is jq's output
// second is '.isalike()' function's output
var a = 
{
  0: [], // #0 true true
  1: new Array, // #1 true true
  2: new Array(10), // #2 true true
  3: {}, // #3 false false
  4: {length:0}, // #4 true true
  5: {length:"0"}, // #5 false true
  6: {length:"asd"}, // #6 false false
  7: {length:false}, // #7 false true
  8: {length:true}, // #8 false true
  9: {length:null}, // #9 false true
 10: {length:0, 0:1}, // #10 true false
 11: {length:"0", 0:1}, // #11 false false
 12: {length:0, 1:1}, // #12 true false
 13: {length:0, 2:1}, // #13 true false
 14: {length:"asd", 0:1}, // #14 false false
 15: {length:0, x1:1}, // #15 true false
 16: {length:0, 0:1, x1:1, x2:1}, // #16 true false
 17: {length:1}, // #17 false true
 18: {length:1, 0:1}, // #18 true true
 19: {length:1, 1:1}, // #19 false false
 20: {length:1, 2:1}, // #20 false false
 21: {length:1, 0:1, x1:1}, // #21 true false
 22: {length:2, 0:1}, // #22 false true
 23: {length:2, 1:1}, // #23 true true
 24: {length:2, 2:1}, // #24 false false
 25: {length:2, 3:1}, // #25 false false
 26: {length:2, 1:1, x1:1}, // #26 true false
 27: window, // #27 false false
 28: document, // #28 false false
 29: function (x1) {}, // #29 false true
 30: document.documentElement, // #30 false false
 31: document.getElementsByTagName("*"), // #31 true false
 32: document.body.childNodes, // #32 true true
 33: document.body.children, // #33 true false
 34: document.querySelectorAll("*"), // #34 true true
 35: document.images, // #35 true true
 36: window.frames, // #36 false false
 37: $(), // #37 true true
 38: $("*"), // #38 true false
 39: "asd", // #39 true false
 40: {length:NaN}, // #40 false false
 41: {length:1/0} // #41 false true
};

console.clear();
for (
  var it = 0, 
  end = 42;
  it < end;
  it++
) {
  console.log( "#"+it, isArraylike(a[it]), isalike(a[it]) );
}
//

最佳答案

I came across dozen of explanations of the thing but every one of them gave diferent point of view...

那是因为有不同的观点。

如果 A) 它是一个数组,或 B) 它不是一个函数,它有一个数字 length 属性,并且 1. 该属性的值为 0,则 jQuery 的函数会考虑类似数组的东西,或 2. 对象具有 length - 1 的键。所以很多稀疏数组不符合该定义(如果它们不是数组的话)。

没有一个技术定义。例如,规范中唯一出现“类数组”的地方是 Annex F。 ,它注意到 §15.3.4.3现在允许将任何具有 length 属性的东西传递给 Function#apply 而不是要求它是数组或 arguments 对象。这比 jQuery 的定义您的定义更开放。

宽泛的定义是:具有长度的东西(我想我们都同意这一点)并且可能具有由所有数字组成的属性名称,这些数字的十进制值(如果存在)落在那个长度,我们有时可能想像对待数组一样对待。例如,arguments 对象。或者一个 jQuery 实例。

关于javascript - 是 'isArraylike' 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21115849/

相关文章:

arrays - 在c中初始化结构成员的 union 时出错

php - 内部动态 JavaScript

javascript - 如何将数组对象转换为数组数组?

javascript - 函数声明以及它们如何在当前范围内创建变量

javascript - jQuery.remove(),分离 DOM 元素,但我仍然可以从代码中访问这些元素。如何避免泄漏?

c - 如何从文件中读取字符直到一个空格并将其存储为一个字符串(c)?

java - 检查两个不同的 ArrayLists 是否相等

php - 来自数组的 PDO bindParam 变量数据类型

javascript - 选择所有具有特定属性值的第一个元素

javascript - 在javascript中迭代数字数组返回字符串