首页 >web前端 >js教程 >ArrayList类(增强版)_基础知识

ArrayList类(增强版)_基础知识

WBOY
WBOY原创
2016-05-16 19:15:39910浏览

Author:月影
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); // 可惜这个值不对,但是没法实现,只好放弃了 <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 是当数组(集合)中的所有元素都满足条件时,返回true,否则返回false
arr.any 是当数组(集合)中的所有元素中任意一个满足条件时,返回true,如果都不满足,返回false
arr.each 返回由符合条件的每一个元素构成的子数组
arr.map 是匹配两个数组(集合)并把它们的元素用指定闭包进行计算 
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn