Created
June 19, 2014 08:48
-
-
Save kontur/1a0ab32d3ccef97d4d06 to your computer and use it in GitHub Desktop.
Javascript Array polyfills
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
Array.prototype.remove = function(from, to) { | |
var rest = this.slice((to || from) + 1 || this.length); | |
this.length = from < 0 ? this.length + from : from; | |
return this.push.apply(this, rest); | |
}; | |
if (!Array.prototype.indexOf){ | |
Array.prototype.indexOf = function(elt /*, from*/) | |
{ | |
var len = this.length >>> 0; | |
var from = Number(arguments[1]) || 0; | |
from = (from < 0) | |
? Math.ceil(from) | |
: Math.floor(from); | |
if (from < 0) | |
from += len; | |
for (; from < len; from++) | |
{ | |
if (from in this && | |
this[from] === elt) | |
return from; | |
} | |
return -1; | |
}; | |
} | |
if (!Array.prototype.map){ | |
Array.prototype.map = function(fun /*, thisArg */) | |
{ | |
"use strict"; | |
if (this === void 0 || this === null) | |
throw new TypeError(); | |
var t = Object(this); | |
var len = t.length >>> 0; | |
if (typeof fun !== "function") | |
throw new TypeError(); | |
var res = new Array(len); | |
var thisArg = arguments.length >= 2 ? arguments[1] : void 0; | |
for (var i = 0; i < len; i++) | |
{ | |
// NOTE: Absolute correctness would demand Object.defineProperty | |
// be used. But this method is fairly new, and failure is | |
// possible only if Object.prototype or Array.prototype | |
// has a property |i| (very unlikely), so use a less-correct | |
// but more portable alternative. | |
if (i in t) | |
res[i] = fun.call(thisArg, t[i], i, t); | |
} | |
return res; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment