ArrayList類(增強版)
更新時間:2007年04月04日 00:00:00 作者:
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); // 可惜這個值不對,但是沒法實現(xiàn),只好放棄了
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 是當數(shù)組(集合)中的所有元素都滿足條件時,返回true,否則返回false
arr.any 是當數(shù)組(集合)中的所有元素中任意一個滿足條件時,返回true,如果都不滿足,返回false
arr.each 返回由符合條件的每一個元素構(gòu)成的子數(shù)組
arr.map 是匹配兩個數(shù)組(集合)并把它們的元素用指定閉包進行計算
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); // 可惜這個值不對,但是沒法實現(xiàn),只好放棄了
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.any 是當數(shù)組(集合)中的所有元素中任意一個滿足條件時,返回true,如果都不滿足,返回false
arr.each 返回由符合條件的每一個元素構(gòu)成的子數(shù)組
arr.map 是匹配兩個數(shù)組(集合)并把它們的元素用指定閉包進行計算
相關(guān)文章
javascript創(chuàng)建和存儲cookie示例
javascript創(chuàng)建和存儲cookie,cookie是存儲于訪問者的計算機中的變量,下面看一下使用示例吧2014-01-01簡介JavaScript中Boolean.toSource()方法的使用
這篇文章主要介紹了簡介JavaScript中Boolean.toSource()方法的使用,是JS入門學習中的基礎知識,需要的朋友可以參考下2015-06-06JavaScript中document.referrer的用法詳解
這篇文章主要給大家介紹了關(guān)于JavaScript中document.referrer的用法,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07JS函數(shù)實現(xiàn)動態(tài)添加CSS樣式表文件
有時會使用一些改變心情方面的想法,比如JS函數(shù)實現(xiàn)動態(tài)添加CSS樣式表文件,這樣就可以做到隨機加載心情文件,需要的朋友可以了解下2012-12-12javascript中arguments,callee,caller詳解
javascript中arguments,caller,callee 是什么? 在javascript 中有什么樣的作用?本篇會對于此做一些基本介紹。希望大家能夠喜歡。2016-03-03