Skip to content

Instantly share code, notes, and snippets.

@aMarCruz
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aMarCruz/b83edda3ec0b463c84a2 to your computer and use it in GitHub Desktop.
Save aMarCruz/b83edda3ec0b463c84a2 to your computer and use it in GitHub Desktop.
Array.prototype.indexOf polyfill by aMarCruz
//
// https://msdn.microsoft.com/library/ff679977(v=vs.94).aspx
// https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/indexOf
//
Array.prototype.indexOf || (Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
'use strict';
if (this == null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length | 0; //to int32
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n !== n || !isFinite(n)) { // para verificar si es NaN
n = 0;
} else if (n !== 0) {
n = Math.floor(n);
if (n < 0) {
n = Math.max(len - Math.abs(n), 0);
}
}
}
for (; n < len; n++) {
if (n in t && t[n] === searchElement) {
return n;
}
}
return -1;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment