Author: Yueying
From:http://bbs.51js.com/thread-66469-1-1.html
<script> <br>function ArrayList() <br>{ <br> var ins = Array.apply(this, arguments); <br> ins.constructor = arguments.callee; <br> ins.base = Array; <br><br> ins.each = function(closure) <br> { <br> if(typeof closure == 'undefined') <br> closure = function(x){return x}; <br> if(typeof closure != 'function') <br> { <br> var c = closure; <br> closure = function(x){return x == c} <br> } <br><br> var ret = new ArrayList(); <br> var args = Array.apply(this, arguments).slice(1); <br><br> for(var i = 0; i < this.length; i ) <BR> { <BR> var rval = closure.apply(this, [this[i]].concat(args).concat(i)) <BR> if(rval || rval === 0) <BR> ret.push(rval); <BR> } <br><br> return ret; <BR> } <br><br> ins.trim = function() <BR> { <BR> return this.each.apply(this); <BR> } <br><br> ins.all = function(closure) <BR> { <BR> return this.each.apply(this, arguments).length == this.length; <BR> } <br><br> ins.any = function(closure) <BR> { <BR> return this.each.apply(this, arguments).length > 0; <br> } <br><br> ins.contains = function(el) <br> { <br> return this.any(function(x){return x == el}); <br> } <br><br> ins.indexOf = function(el) <br> { <br> var ret = this.each.call(this, function(x, i){return el == x?i:false})[0]; <br> return ret ? ret : -1; <br> } <br><br> ins.subarr = function(start, end) <br> { <br> end = end || Math.Infinity; <br> return this.each.call(this, function(x, i){return i >= start && i < end ? x : null}); <BR> } <br><br> ins.valueOf = ins.toString; <br><br> ins.toString = function() <BR> { <BR> return '[' this.valueOf() ']'; <BR> } <br><br> ins.map = function(list, closure) <BR> { <BR> if (typeof list == 'function' && typeof closure != 'function') <BR> { <BR> var li = closure; <BR> closure = list; <BR> list = li; <BR> } <BR> closure = closure || ArrayList; <br><br> return this.each.call(this, function(x, i){return closure.call(this, x, list[i])}); <BR> }; <br><br> ins.slice = function() <BR> { <BR> return this.constructor(ins.base.prototype.slice.apply(this, arguments)); <BR> } <br><br> ins.splice = function() <BR> { <BR> return this.constructor(ins.base.prototype.splice.apply(this, arguments)); <BR> } <br><br> ins.concat = function() <BR> { <BR> return this.constructor(ins.base.prototype.concat.apply(this, arguments)); <BR> }<br><br> return ins; <BR>} <br><br>var a = new ArrayList(1,2,3); <BR>alert(a.length); <BR>alert(a); <BR>alert(a instanceof Array); <BR>alert(a.constructor); <BR>alert(a instanceof ArrayList); // Unfortunately, this value is wrong, but it cannot be implemented, so I had to give up. <br><br>alert(a.each(function(x){return x x})); <BR>alert(a.all(function(x){return x>0})); <br>alert(a.all(function (x){return x<1})); <BR>alert(a.any(function(x){return x == 2})); <br><br>alert(a.contains(2)) ; <BR>alert(a.contains(-1)); <br><br>var b = a.map([3,2], function(x, y){return x y}); <BR>alert (b); <BR>alert(a.map([2,3,4])); <br><br>alert(a.indexOf(2)); <BR>alert(a.indexOf(-1 )); <br><br>alert(a.subarr(1,3)); <BR>alert(a.toString()); <BR>var b = new ArrayList(a,a); <BR> alert(b.toString()); <BR>alert(b.slice(1)); <BR></script>
arr.all is when all elements in the array (collection) are When the condition is met, return true, otherwise return false
arr.any is when any one of all elements in the array (set) meets the condition, return true, if none of the elements are met, return false
arr.each Return The subarray
arr.map composed of each element that meets the conditions matches two arrays (sets) and calculates their elements using the specified closure.