Use array.indexOf native function when available. p=arno, r=ahocevar

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9859 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Frédéric Junod
2009-12-04 08:07:53 +00:00
parent 610c2b9933
commit 4ff1ece2e4
2 changed files with 18 additions and 6 deletions

View File

@@ -147,17 +147,21 @@ OpenLayers.Util.clearArray = function(array) {
* obj - {Object}
*
* Returns:
* {Integer} The index at, which the object was found in the array.
* {Integer} The index at, which the first object was found in the array.
* If not found, returns -1.
*/
OpenLayers.Util.indexOf = function(array, obj) {
for(var i=0, len=array.length; i<len; i++) {
if (array[i] == obj) {
return i;
// use the build-in function if available.
if (typeof array.indexOf == "function") {
return array.indexOf(obj);
} else {
for (var i = 0, len = array.length; i < len; i++) {
if (array[i] == obj) {
return i;
}
}
return -1;
}
return -1;
};

View File

@@ -38,6 +38,14 @@
"getImagesLocation()" );
}
function test_Util_IndexOf(t) {
t.plan( 3 );
var array = new Array(1, "bar");
t.eq(OpenLayers.Util.indexOf(array, 1), 0);
t.eq(OpenLayers.Util.indexOf(array, "bar"), 1);
t.eq(OpenLayers.Util.indexOf(array, "foo"), -1);
}
function test_Util_Array(t) {
t.plan( 2 );