Array.from在jQuery中实现

时间:2011-08-19 21:20:32

标签: jquery arrays

https://github.com/sstephenson/prototype/blob/1fb9728/src/lang/array.js#L104

我需要将Array.from()方法与原型js分开,并在jQuery中实现它。任何想法从哪里开始?

1 个答案:

答案 0 :(得分:1)

$ A只是此函数的变量:

function $A(iterable) {
  if (!iterable) return [];
  // Safari <2.0.4 crashes when accessing property of a node list with property accessor.
  // It nevertheless works fine with `in` operator, which is why we use it here
  if ('toArray' in Object(iterable)) return iterable.toArray();
  var length = iterable.length || 0, results = new Array(length);
  while (length--) results[length] = iterable[length];
  return results;
}
Array.from = $A;
...
Array.from("a,b,c,e,d") // outputs: ["a", ",", "d", ",", "f", ",", "e", ",", "e"]