なんだかんだでバグが多かったので。一応バグfixと、コード追加。
でもちゃんぽんなことに変わりなし。
あくまで参考に。
前回:
http://d.hatena.ne.jp/Kureduki_Maari/20090620/1245494707
/*============================================================ * Array 20090807 * c4se project - http://c4se.sakura.ne.jp * * project name: c4seJS * author: ne_Schirou - utakata.c4se@gmail.com * <c4seFCL 0.2> ============================================================*/ (function(){ const MAX_LENGTH = 4294967295; var abs = Math.abs, ceil = Math.ceil, floor = Math.floor, random = Math.random; Array = function(/*arguments*/){ if(arguments.length == 1 && arguments[0]._className == "Number"){ //arguments[0] = floor(abs(arguments[0])); this._private = {}; if(arguments[0] > MAX_LENGTH) throw RangeError("Array(): The length is too big."); this._private.length = arguments[0]; }else{ if(arguments.length > MAX_LENGTH) throw RangeError("Array(): The length is too big."); this._private.length = arguments.length; for(var i=0, l=arguments.length; i<l; ++i) this[i] = arguments[i]; } return this; }; /*generic intrinsic function +(a:Array, b:Array){//Ex return a.concat(b); }; generic intrinsic function *(a:Array, n:Number){//Ex return a.repeat(n); }; generic intrinsic function -@(a:Array){//Ex return a.map(function(elm){reutrn -elm;}); }; generic intrinsic function -(a:Array, b:Array){//Ex return a.filter(function(elm){ return b.indexOf(elm) > 0? true: false; }); };*/ Array.prototype._className = "Array";//Ex /*Object.defineProperty(Array.prototype, '[]', { getter: function(n){ n = ((n > 0)? floor(n): ceil(n)); while(n < 0) n += this.length; return this[n]; }, setter: function(n, v){ n = ((n > 0)? floor(n): ceil(n)); if(n > MAX_LENGTH) throw RangeError("Array.prototype.[]=: The length is too big."); while(n < 0) n += this.length; if(n > this.length) this.length = n; this[n] = v; return this; } });*/ /*Object.defineProperty(Array.prototype, 'length', { getter: function(){return this._private.length;}, setter: function(n){ n = floor(abs(n)); if(n > MAX_LENGTH) throw RangeError("Array.prototype.length=: The length is too big."); if(this._private.length > n) for(var i=n+1, l=this._private.length; i<l; ++i) this[i] = undefined; this._private.length = n; return this; } });*/ /*Array.prototype.clean = function(){//Ex //return this.filter(function(elm){return elm != null ? true : false;}); var i = 0, l = this.length, a = new Array(0); for(; i<l; ++i) if(a[i] != null) a[a.length] = this[i]; return a; };*/ /*Array.prototype.clone = function(){//Ex var i = 0, l = this.length, a = new Array(); for(; i<l; ++i){ if(this[i] === undefined) continue; else if(this[i]._className != "Array") a[a.length] = this[i]; else a[a.length] = this[i].clone(); } return a; };*/ Array.prototype.concat = function(/*array|arguments*/){ var i = 0, l = arguments.length, a = this.clone(), j = 0, l_2 = 0; for(; i<l; ++i){ if(arguments[i]._className != "Array") a[a.length] = arguments[i]; else for(j=0, l_2=arguments[i].length; j<l_2; ++j) a[a.length] = arguments[i][j]; } return a; }; Array.prortotype.every = function(f, o){ var i = 0, l = this.length; for(; i<l; ++i){ if(this[i] === undefined) continue; if(!f.call(o, this[i], i, this)) return false; } return true; }; Array.prototype.filter = function(f, o){ var a = new Array(); for(var i=0, l=this.length; i<l; ++i){ if(this[i] === undefined) continue; if(f.call(o, this[i], i, this)) a[a.length] = this[i]; } return a; }; /*Array.prototype.flat = function(){//Ex var i = 0, l = this.length, a = new Array(0); for(; i<l; ++i){ if(this[i]._className != "Array") a[a.length] = this[i]; else a[a.length] = a.flat(); } return a; };*/ Array.prototype.forEach = function(f, o){ var i = 0, l = this.length; for(; i<l; ++i){ if(this[i] === undefined) continue; f.call(o, this[i], i, this); } return this; }; Array.prototype.indexOf = function(v, n){ n = n || 0; var i = 0, l = this.length; while(n < 0) n += l - 1; for(i=n; i<l; ++i) if(this[i] === v) return i; return -1; }; Array.prototype.join = function(s){ if(s === undefined) s = ","; var str = this[0].toString(); for(var i=1; i<this.length; ++i) str += this[i].toString(); return str; }; Array.prototype.lastIndexOf = function(v, n){ if(n === undefined) n = this.length - 1; while(n < 0) n += this.length - 1; var i = n; for(; i>=0; --i) if(this[i] === v) return i; return -1; }; Array.prototype.map = function(f, o){ var i = 0, l = this.length; a = new Array(l); for(; i<l; ++i){ if(this[i] === undefined) continue; a[i] = f.call(o, this[i], i, this); } return a; }; Array.prototype.pop = function(){ var l = this.length, v = this[l-1]; this[l-1] = undefined; --this.length; return v; }; Array.prototype.push = function(/*arguments*/){ var i = 0, l = arguments.length; for(; i<l; ++i) this[this.length] = arguments[i]; return this.length; }; /*Array.prototype.random = function(){//Ex return this[floor(random() * this.length)]; };*/ Array.prototype.reduce = function(f, v){ var i = 0, l = this.length; if(v === undefined){v = this[0]; i = 1;} for(; i<l; ++i){ if(this[i] === undefined) continue; v = f.call(null, v, this[i], i, this); } return v; }; Array.prototype.reduceRight = function(f, v){ var i = this.length - 1; if(v === undefined){v = this[i]; --i;} for(; i>=0; --i){ if(this[i] === undefined) continue; v = f.call(null, v, this[i], i, this); } return v; }; /*Array.prototype.repeat = function(n){//Ex n = abs(floor(Number(n))); var r = new Array(), a = this.clone(); for(; n>0; n>>>=1, a.concat(a)) if (n & 1) r.concat(a); return r; };*/ Array.prototype.reverse = function(){ var i = 0, l = this.length, a = new Array(l); for(; i<l; ++i) a[i] = this[l-i]; return this = a; }; Array.prototype.shift = function(){ var i = 0, l = this.length - 1, v = this[0], a = new Array(l); for(; i<l; ++i) a[i] = this[i+1]; this = a; return v; }; Array.prototype.slice = function(m, n){ m = m || 0; n = n || this.length; var i = 0; for(; i<2; ++i) while(arguments[i] < 0) arguments[i] += this.length; if(m >= n) return []; var a = new Array(); for(i=m; i<n; ++i) a[a.length] = this[i]; return a; }; Array.prototype.some = function(f, o){ var i = 0, l = this.length; for(; i<l; ++i){ if(this[i] === undefined) continue; if(f.call(o, this[i], i, this)) return true; } return false; }; (function(){ var _def = function(l, r){ // default comparison function (by String charctor code) var i = 0, len = l.length; l = l.toString().split(''); r = l.toString().split(''); for(; i<len; ++i){ if(l[i].charCodeAt(0) > r[i].charCodeAt(0)) return -1; if(l[i].charCodeAt(0) < r[i].charCodeAt(0)) return 1; } return 0; }; Array.prototype.sort = function(f){ if(this.length == 0) return this; var car = this[0], cdr = this.slice(1); f = f || _def; return cdr.filter(function(elm){ return f(car, elm) < 0; }, this).sort(f) .concat(car) .concat(cdr.filter(function(elm){ return f(car, elm) == 0; }, this)) .concat(cdr.filter(function(e){ return f(car, elm) > 0; }, this).sort(f)); }; })(); Array.prototype.splice = function(m, n /*, arguments*/){ var i = 0, l = this.length, argl = arguments.length, a = new Array(), re = new Array(); n = n || l; for(; i<2; ++i) while(arguments[i] < 0) arguments[i] += this.length; for(i=0; i<m; ++i) a[a.length] = this[i]; for(i=m; i<m+n; ++i) re[re.length] = this[i]; for(i=2; i<argl; ++i) a[a.length] = arguments[i]; for(i=m+n; i<l; ++i) a[a.length] = this[i]; if(n == 1) re = this[m]; this = a; return re; }; Array.prototype.toLocaleString = function(){ var s = "「" + this[0].toLocaleString(); for(var i=0, l=this.length; i<l; ++i) str += "、" + this[i].toLocaleString(); return str + "」"; }; Array.prototype.toArray = function(){return this;};//Ex Array.prototype.toNumber = function(){return this.length;};//Ex Array.prototype.toString = function(){return this.join();};//Ex /*Array.prototype.unique = function(f){//Ex if(!f) f = function(r, l){return r == l;}; var a = [], i = 0, j = 0, l = this.length, k = a.length, tf = true; for(; i<l; ++i){ if(this[i] === undefined) continue; for(j=0, k=a.length, tf=true; j<k; ++j) if(f(a[j], this[i])){ tf = false; break; } if(tf) a[a.length] = this[i]; } return a; };*/ Array.prototype.unshift = function(/*arguments*/){ var i = arguments.length, l = this.length, j = l; for(; i>0; --i){ for(j=l; j>0; --j) this[j] = this[j-1]; this[0] = arguments[i]; } return this.length; }; })();