XArray

(function(ns){

	var __proto__ = Array.prototype;
	
	var shim = {
	
		forEach: function (fn, context) {
			var i, len;
			for (i = 0, len = this.length; i < len; ++i) {
				if (i in this) {
					fn.call(context, this[i], i, this);
				}
			}
		},
		
		indexOf: function (searchElement , fromIndex) {
			var i,
				pivot = (fromIndex) ? fromIndex : 0,
				length;

			if (!this) {
			  throw new TypeError();
			}

			length = this.length;

			if (length === 0 || pivot >= length) {
			  return -1;
			}

			if (pivot < 0) {
			  pivot = length - Math.abs(pivot);
			}

			for (i = pivot; i < length; i++) {
			  if (this[i] === searchElement) {
				return i;
			  }
			}
			return -1;
		}
		
	};
	
	
	function XArray(){
		this.apply(arguments);
		//this.__proto__ = __proto__; //ie > 10
	}
	
	//XArray.prototype = [];// ie<8, length = 0
	XArray.prototype = {
		
		length:0,
		
		push:__proto__.push,
		
		slice:__proto__.slice,
		
		sort: __proto__.sort,
		
		splice: __proto__.splice,
		
		forEach: __proto__.forEach || shim.forEach,
		
		indexOf: __proto__.indexOf || shim.indexOf,
		
		apply: function(array){
			this.push.apply(this, array);
		}
	};

	ns.XArray = XArray;
	
	//var x = new XArray("a","b","c");
	
	//x.forEach(function(i, j){
		//alert(i);
	//})
	//alert(x.length);//3
	//alert(__proto__.toString.call(x))
	
}(this));

  

posted @ 2013-12-02 18:17  rentj  阅读(408)  评论(0)    收藏  举报