Return to Snippet

Revision: 27752
at June 22, 2010 14:07 by mrhoo


Initial Code
(function(){
	var p, ap= Array.prototype,
	p2={
		filter: function(fun, scope){
			var L= this.length, A= [], i= 0, val;
			if(typeof fun== 'function'){
				while(i< L){
					if(i in this){
						val= this[i];
						if(fun.call(scope, val, i, this)){
							A[A.length]= val;
						}
					}
					++i;
				}
			}
			return A;
		},
		forEach: function(fun, scope){
			var L= this.length, i= 0;
			if(typeof fun== 'function'){
				while(i< L){
					if(i in this){
						fun.call(scope, this[i], i, this);
					}
					++i;
				}
			}
			return this;
		},
		every: function(fun, scope){
			var L= this.length, i= 0;
			if(typeof fun== 'function'){
				while(i<L){
					if(i in this && !fun.call(scope, this[i], i, this)) return false;
					++i;
				}
				return true;
			}
			return null;
		},
		indexOf: function(what, i){
			i= i || 0;
			var L= this.length;
			while(i< L){
				if(this[i]=== what) return i;
				++i;
			}
			return -1;
		},
		lastIndexOf: function(what, i){
			var L= this.length;
			i= i || L-1;
			if(isNaN(i) || i>= L) i= L-1;
			else if(i< 0) i += L;
			while(i> -1){
				if(this[i]=== what) return i;
				--i;
			}
			return -1;
		},
		map: function(fun, scope){
			var L= this.length, A= Array(this.length), i= 0, val;
			if(typeof fun== 'function'){
				while(i< L){
					if(i in this){
						A[i]= fun.call(scope, this[i], i, this);
					}
					++i;
				}
				return A;
			}
		},
		some: function(fun, scope){
			var i= 0, L= this.length;
			if(typeof fun== 'function'){
				while(i<L){
					if(i in this && fun.call(scope, this[i], i, this)) return true;
					++i;
				}
				return false;
			}
		}
	}
	for(p in p2){
		if(!ap[p]) ap[p]= p2[p];
	}
})();

Initial URL


Initial Description


Initial Title
extending ecmascript 5 array methods

Initial Tags
javascript, array, html5

Initial Language
JavaScript