且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该使用jQuery.inArray()吗?

更新时间:2023-11-10 12:04:34

内部内部 inArray 做一个简单的循环,我建议您检查是否有原生的 Array.prototype.indexOf 实现并使用它而不是 inArray (如果可用):

Well internally inArray makes a simple loop, I would recommend you to check if there is a native Array.prototype.indexOf implementation and use it instead of inArray if available:

function findPoint(point, list) {
  var l = list.map(function anonMapToId(p) { return p.id });
  var found = ('indexOf' in Array.prototype) ? l.indexOf(point.id)
                                             : jQuery.inArray(point.id, l);
  return found;
}

Array.prototype.indexOf 方法已经在实现JavaScript 1.6的浏览器中引入,它将成为ECMAScript 5标准的一部分。

The Array.prototype.indexOf method has been introduced in browsers that implement JavaScript 1.6, and it will be part of the ECMAScript 5 standard.

本机实现是方式比非本地的。

Native implementations are way faster than non native ones.