当前位置: 首页 > 图文教程 > 网络编程 > Javascript > ArrayList类(增强版)

Javascript
非常厉害的javascript 实现指针式时间
文档处理系列——随时更新
用javascript实现文本框和"选择"按扭之间的间距
使用户点击后退按钮使效三行代码
Javascript 个人笔记(没有整理,很乱)
建议大家看下JavaScript重要知识更新
javascript实现操作cookie实现的可记忆菜单
javascript下for循环用法小结
一个快速添加标签的小玩意,但在FF下不能用,望高手指点
HTML中事件触发列表与解说
极品源码:自动完成 仿163篇
doctype后如何获得body.clientHeight的方法
用javascript调出windows色版的代码
js实现的网页颜色代码表全集
javascript实现爱你在FF IE下都有效的添加一个项目
js实现用滚动条来放大缩小图片的代码
javascript实现unicode和字符的互相转换
符合W3C网页标准的iframe标签的使用方法
firefox下javascript实现高亮关键词的方法
用javascript实现读取txt文档的脚本

Javascript 中的 ArrayList类(增强版)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 265 ::
收藏到网摘: n/a

Author:月影
From:http://bbs.51js.com/thread-66469-1-1.html
复制代码 代码如下:

<script>
function ArrayList()
{
var ins = Array.apply(this, arguments);
ins.constructor = arguments.callee;
ins.base = Array;
ins.each = function(closure)
{
if(typeof closure == 'undefined')
closure = function(x){return x};
if(typeof closure != 'function')
{
var c = closure;
closure = function(x){return x == c}
}
var ret = new ArrayList();
var args = Array.apply(this, arguments).slice(1);
for(var i = 0; i < this.length; i++)
{
var rval = closure.apply(this, [this[i]].concat(args).concat(i))
if(rval || rval === 0)
ret.push(rval);
}
return ret;
}
ins.trim = function()
{
return this.each.apply(this);
}
ins.all = function(closure)
{
return this.each.apply(this, arguments).length == this.length;
}
ins.any = function(closure)
{
return this.each.apply(this, arguments).length > 0;
}
ins.contains = function(el)
{
return this.any(function(x){return x == el});
}
ins.indexOf = function(el)
{
var ret = this.each.call(this, function(x, i){return el == x?i:false})[0];
return ret ? ret : -1;
}
ins.subarr = function(start, end)
{
end = end || Math.Infinity;
return this.each.call(this, function(x, i){return i >= start && i < end ? x : null});
}
ins.valueOf = ins.toString;
ins.toString = function()
{
return '['+this.valueOf()+']';
}
ins.map = function(list, closure)
{
if (typeof list == 'function' && typeof closure != 'function')
{
var li = closure;
closure = list;
list = li;
}
closure = closure || ArrayList;
return this.each.call(this, function(x, i){return closure.call(this, x, list[i])});
};
ins.slice = function()
{
return this.constructor(ins.base.prototype.slice.apply(this, arguments));
}
ins.splice = function()
{
return this.constructor(ins.base.prototype.splice.apply(this, arguments));
}
ins.concat = function()
{
return this.constructor(ins.base.prototype.concat.apply(this, arguments));
}
return ins;
}
var a = new ArrayList(1,2,3);
alert(a.length);
alert(a);
alert(a instanceof Array);
alert(a.constructor);
alert(a instanceof ArrayList); // 可惜这个值不对,但是没法实现,只好放弃了
alert(a.each(function(x){return x+x}));
alert(a.all(function(x){return x>0}));
alert(a.all(function(x){return x<1}));
alert(a.any(function(x){return x == 2}));
alert(a.contains(2));
alert(a.contains(-1));
var b = a.map([3,2], function(x, y){return x+y});
alert(b);
alert(a.map([2,3,4]));
alert(a.indexOf(2));
alert(a.indexOf(-1));
alert(a.subarr(1,3));
alert(a.toString());
var b = new ArrayList(a,a);
alert(b.toString());
alert(b.slice(1));
</script>
arr.all 是当数组(集合)中的所有元素都满足条件时,返回true,否则返回false
arr.any 是当数组(集合)中的所有元素中任意一个满足条件时,返回true,如果都不满足,返回false
arr.each 返回由符合条件的每一个元素构成的子数组
arr.map 是匹配两个数组(集合)并把它们的元素用指定闭包进行计算