Skip to content

Instantly share code, notes, and snippets.

@lgmcolin
Created January 16, 2014 07:37
Show Gist options
  • Save lgmcolin/8451118 to your computer and use it in GitHub Desktop.
Save lgmcolin/8451118 to your computer and use it in GitHub Desktop.
ECMAScript array method,兼容ie6-8
/**
* ES5中新增数组方法的兼容实现
* method:// forEach map filter some every indexof lastindexof reduce reduceRight
*/
if(typeof Array.prototype.forEach != 'function'){
Array.prototype.forEach = function(fn,context){
if(typeof fn === 'function'){
for(var i = 0,length = this.length; i<length; i++){
fn.call(context,this[i],i,this);
}
}
}
}
if(typeof Array.prototype.map != 'function'){
Array.prototype.map = function(fn,context){
var arr = [];
if(typeof fn === 'function'){
for(var i = 0,length = this.length;i < length; i++){
arr.push(fn.call(context,this[i],i,this));
}
}
}
}
if(typeof Array.prototype.filter != 'function'){
Array.prototype.filter = function(fn,context){
var arr = [];
if(typeof fn === 'function'){
for(var i = 0,length = this.length;i < length; i++){
fn.call(context,this[i],i,this) && arr.push(this[k]);
}
}
}
}
if(typeof Array.prototype.some != 'function'){
Array.prototype.some = function(fn,context){
var passed = false;
if(typeof fn === 'function'){
for(var i = 0,length = this.length;i < length; i++){
if(passed) break;
passed = !!fn.call(context,this[i],i,this);
}
}
return passed;
}
}
if(typeof Array.prototype.every != 'function'){
Array.prototype.every = function(fn,context){
var passed = true;
if(typeof fn === 'function'){
for(var i = 0,length = this.length;i < length; i++){
if(passed === false) break;
passed = !!fn.call(context,this[i],i,this);
}
}
return passed;
}
}
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;
};
}
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;
};
}
if (typeof Array.prototype.reduce != "function") {
Array.prototype.reduce = function (callback, initialValue ) {
var previous = initialValue, k = 0, length = this.length;
if (typeof initialValue === "undefined") {
previous = this[0];
k = 1;
}
if (typeof callback === "function") {
for (k; k < length; k++) {
this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
}
}
return previous;
};
}
if (typeof Array.prototype.reduceRight != "function") {
Array.prototype.reduceRight = function (callback, initialValue ) {
var length = this.length, k = length - 1, previous = initialValue;
if (typeof initialValue === "undefined") {
previous = this[length - 1];
k--;
}
if (typeof callback === "function") {
for (k; k > -1; k-=1) {
this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
}
}
return previous;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment