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 是匹配两个数组(集合)并把它们的元素用指定闭包进行计算
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 是匹配两个数组(集合)并把它们的元素用指定闭包进行计算
标签:
ArrayList类(增强版)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“ArrayList类(增强版)”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
2024年11月14日
2024年11月14日
- 刘德华《劲歌集》24K金碟港版[WAV+CUE][1.2G]
- Faker五冠王什么意思 世界赛五冠王Faker选手介绍
- faker塞拉斯s14决赛什么出装 faker塞拉斯s14决赛出装介绍
- LOLtoc9魔法口令在哪可以找到 2024云顶之弈toc观赛魔法口令兑换码
- 黑鸭子2008《影视经典·珍藏版》试音碟[WAV+CUE]
- 碧娜《温柔吻语2》[WAV+CUE]
- Stravinsky-Symphonies,Volume1-OrquestaSinfonicadeGalicia,DimaSlobodeniouk(2024)[24-
- 外媒评Switch2:向下兼容是关键 但挑战依然存在
- 任天堂提醒:宠物的尿或唾液或会让NS故障 把它放好!
- 《博德3》再创新高 Steam掌机总游玩时长近2000年
- 张玮伽《想你的夜DSD》东升 [WAV+CUE][1G]
- 姚璎格《 粤 24KGOLD》正版低速原抓[WAV+CUE][1G]
- 杨千嬅《如果大家都拥有海》寰亚 [WAV+CUE][998M]
- 孟庭苇.1994-1990-1994钻石精选集2CD(2022环球XRCD限量版)【上华】【WAV+CUE】
- 群星.1998-华纳好情歌精选17首【华纳】【WAV+CUE】