Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Array.prototype.indexOf for JScript

Closes #18.
  • Loading branch information...
commit dd21d875f08f22442bdca93a9cc6c7fbf2bfd8fa 1 parent a8ebd4f
@lautis authored
Showing with 35 additions and 1 deletion.
  1. +35 −1 lib/es5.js
View
36 lib/es5.js
@@ -150,4 +150,38 @@ if (!Array.prototype.reduce)
return accumulator;
};
-}
+}
+
+// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
+if (!Array.prototype.indexOf) {
+ Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
+ "use strict";
+ if (this === void 0 || this === null) {
+ throw new TypeError();
+ }
+ var t = Object(this);
+ var len = t.length >>> 0;
+ if (len === 0) {
+ return -1;
+ }
+ var n = 0;
+ if (arguments.length > 0) {
+ n = Number(arguments[1]);
+ if (n !== n) { // shortcut for verifying if it's NaN
+ n = 0;
+ } else if (n !== 0 && n !== Infinity && n !== -Infinity) {
+ n = (n > 0 || -1) * Math.floor(Math.abs(n));
+ }
+ }
+ if (n >= len) {
+ return -1;
+ }
+ var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
+ for (; k < len; k++) {
+ if (k in t && t[k] === searchElement) {
+ return k;
+ }
+ }
+ return -1;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.