Skip to content

Instantly share code, notes, and snippets.

@darthwade
Created July 20, 2014 10:54
Show Gist options
  • Save darthwade/5adf1409aa32421c0184 to your computer and use it in GitHub Desktop.
Save darthwade/5adf1409aa32421c0184 to your computer and use it in GitHub Desktop.
Polyfill for Array.indexOf(searchElement, fromIndex)
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
var k;
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
if (len === 0) {
return -1;
}
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
var kValue;
if (k in O && O[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment