Created
September 28, 2013 07:24
-
-
Save xyqfer/6739495 to your computer and use it in GitHub Desktop.
Array.prototype
This file contains hidden or 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
| // forEach | |
| if (typeof Array.prototype.forEach != "function") { | |
| Array.prototype.forEach = function (fn, context) { | |
| for (var k = 0, length = this.length; k < length; k++) { | |
| if (typeof fn === "function") { | |
| fn.call(context, this[k], k, this); | |
| } | |
| } | |
| }; | |
| } | |
| // map | |
| if (typeof Array.prototype.map != "function") { | |
| Array.prototype.map = function (fn, context) { | |
| var arr = []; | |
| if (typeof fn === "function") { | |
| for (var k = 0, length = this.length; k < length; k++) { | |
| arr.push(fn.call(context, this[k], k, this)); | |
| } | |
| } | |
| return arr; | |
| }; | |
| } | |
| // filter | |
| if (typeof Array.prototype.filter != "function") { | |
| Array.prototype.filter = function (fn, context) { | |
| var arr = []; | |
| if (typeof fn === "function") { | |
| for (var k = 0, length = this.length; k < length; k++) { | |
| fn.call(context, this[k], k, this) && arr.push(this[k]); | |
| } | |
| } | |
| return arr; | |
| }; | |
| } | |
| // some | |
| if (typeof Array.prototype.some != "function") { | |
| Array.prototype.some = function (fn, context) { | |
| var passed = false; | |
| if (typeof fn === "function") { | |
| for (var k = 0, length = this.length; k < length; k++) { | |
| if (passed === true) break; | |
| passed = !!fn.call(context, this[k], k, this); | |
| } | |
| } | |
| return passed; | |
| }; | |
| } | |
| // every | |
| if (typeof Array.prototype.every != "function") { | |
| Array.prototype.every = function (fn, context) { | |
| var passed = true; | |
| if (typeof fn === "function") { | |
| for (var k = 0, length = this.length; k < length; k++) { | |
| if (passed === false) break; | |
| passed = !!fn.call(context, this[k], k, this); | |
| } | |
| } | |
| return passed; | |
| }; | |
| } | |
| // indexOf | |
| if (typeof Array.prototype.indexOf != "function") { | |
| Array.prototype.indexOf = function (searchElement, fromIndex) { | |
| var index = -1; | |
| fromIndex = fromIndex * 1 || 0; | |
| for (var k = 0, length = this.length; k < length; k++) { | |
| if (k >= fromIndex && this[k] === searchElement) { | |
| index = k; | |
| break; | |
| } | |
| } | |
| return index; | |
| }; | |
| } | |
| // lastIndexOf | |
| if (typeof Array.prototype.lastIndexOf != "function") { | |
| Array.prototype.lastIndexOf = function (searchElement, fromIndex) { | |
| var index = -1, length = this.length; | |
| fromIndex = fromIndex * 1 || length - 1; | |
| for (var k = length - 1; k > -1; k-=1) { | |
| if (k <= fromIndex && this[k] === searchElement) { | |
| index = k; | |
| break; | |
| } | |
| } | |
| return index; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment