Created
February 21, 2014 18:56
-
-
Save jcreamer898/9140822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!Array.prototype.indexOf) { | |
Array.prototype.indexOf = function (searchElement, fromIndex) { | |
if ( this === undefined || this === null ) { | |
throw new TypeError( '"this" is null or not defined' ); | |
} | |
var length = this.length >>> 0; // Hack to convert object.length to a UInt32 | |
fromIndex = +fromIndex || 0; | |
if (Math.abs(fromIndex) === Infinity) { | |
fromIndex = 0; | |
} | |
if (fromIndex < 0) { | |
fromIndex += length; | |
if (fromIndex < 0) { | |
fromIndex = 0; | |
} | |
} | |
for (;fromIndex < length; fromIndex++) { | |
if (this[fromIndex] === searchElement) { | |
return fromIndex; | |
} | |
} | |
return -1; | |
}; | |
} |
The in
check would be:
if (fromIndex in this && this[fromIndex] === searchElement) {
This polyfill is missing other steps, like converting this
to an object, that es5-shim covers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For reference here is es5-shim's version:
https://github.com/es-shims/es5-shim/blob/29634c271a44889770b73abc2c49bba29e47b960/es5-shim.js#L602-L627