您好,欢迎访问一九零五行业门户网

JavaScript程序中Array数组对象的扩展函数实例

我们经常给 string,function,array 的原型加上自定义的扩展函数,比如去除字符串空格,数组排序等
今天重点讲下 如何给array对象扩展
1、直接在array.prototype 上扩展
2、用自己方法对数组对象进行扩展
直接在array.prototype上扩展,不能直接对dom对象使用(如:document.getelementsbytagname('div')得到的nodelist);
对有洁癖的同学而言 也破了原始生态环境的 : )
先来看下 yui操作数组的一些方法,这里我对源码简单剥离并改动了下
(function(){
var yarray;
yarray = function(o,idx,arraylike){
var t = (arraylike) ? 2 : yarray.test(o),
l, a, start = idx || 0;
if (t) {
try {
return array.prototype.slice.call(o, start); //借助array原生方法来把aguments转换为js数组
} catch(e) {
a = [];
l = o.length;
for (; start<l; start++) {
a.push(o[start]);
}
return a;
}
} else {
return [o];
}
}
yarray.test = function(o){
var r = 0;
if (o && (typeof o == 'object' ||typeof o == 'function')) {
if (object.prototype.tostring.call(o) === "[object array]") {
r = 1;
} else {
try {
if (('length' in o) && !o.tagname && !o.alert && !o.apply) {
r = 2;
}
} catch(e) {}
}
}
return r;
}
yarray.each = (array.prototype.foreach) ? //先检测浏览器是否已支持,若有则调用原生
function (a, f, o) {
array.prototype.foreach.call(a || [], f, o || y);
return yarray;
} :
function (a, f, o) {
var l = (a && a.length) || 0, i;
for (i = 0; i < l; i=i+1) {
f.call(o || y, a[i], i, a);
}
return yarray;
};
yarray.hash = function(k, v) {
var o = {}, l = k.length, vl = v && v.length, i;
for (i=0; i<l; i=i+1) {
if (k[i]) {
o[k[i]] = (vl && vl > i) ? v[i] : true;
}
}
return o;
};
yarray.indexof = (array.prototype.indexof) ?
function(a, val) {
return array.prototype.indexof.call(a, val);
} :
function(a, val) {
for (var i=0; i<a.length; i=i+1) {
if (a[i] === val) {
return i;
}
}
return -1; //寻找不到的情况
};
yarray.numericsort = function(a, b) {
return (a - b); //从小到大排序, return (b - a); 从大到小
};
yarray.some = (array.prototype.some) ?
function (a, f, o) {
return array.prototype.some.call(a, f, o);
} :
function (a, f, o) {
var l = a.length, i;
for (i=0; i<l; i=i+1) {
if (f.call(o, a[i], i, a)) {
return true;
}
}
return false;
};
})();
借助array原生方法来把aguments转换为js数组的其他方法 (dom对象不可以,只有遍历)
array.apply(null,arguments);
[].slice.call(arguments,0);
[].splice.call(arguments,0,arguments.length);
[].concat.apply([],arguments);
...
yarray函数不仅可以操作数组对象也对nodelist对象进行了操作
yarray(document.getelementsbytagname("div"));
遍历dom对象 重新组装成一个数组 : )
a = [];
l = o.length;
for (; start<l; start++) {
a.push(o[start]);
}
return a;
yarray.each
遍历数组,如有传入函数,每次遍历都执行callback
yarray.each([1,2,3],function(item){
alert(item);// 执行了3次,1,2,3
});
yarray.hash
数组 组装成 键值对 可以理解成一个json对象
yarray.hash(["a","b"],[1,2]);
yarray.indexof
返回(想要找寻值)一样的该值在数组的索引值
yarray.indexof([1,2],1)
yarray.numericsort
对数组进行排序,从小到大
[3, 1, 2].sort(yarray.numericsort);
yarray.some
是否数组中的有元素通过了callback的处理?如果有,则立马返回true,如果一个都没有,则返回false
yarray.some([3, 1, 2],function(el){
return el < 4;
})
让我们看看 javascript 1.6 -1.8 对数组的扩展 ,并学习如何实现相同的功能
every
filter
foreach
indexof
lastindexof
map
some
reduce
reduceright
array.prototype.every
if (!array.prototype.every)
{
array.prototype.every = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != function)
throw new typeerror();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this &&
!fun.call(thisp, this[i], i, this))
return false;
}
return true;
};
}
是否数组中的每个元素都通过了callback的处理?如果是,则返回true,如果有一个不是,则立马返回false
这和我们刚才提到的yui种的 some 函数 很雷同 :) 功能刚好相反
array.prototype.filter
array.prototype.filter = function (block /*, thisp */) { //过滤器 ,添加方便,进行判断过滤
var values = [];
var thisp = arguments[1];
for (var i = 0; i < this.length; i++)
if (block.call(thisp, this[i]))
values.push(this[i]);
return values;
};
使用方法
var val= numbers.filter(function(t){
return t < 5 ;
})
alert(val);
foreach 和 indexof 和 some 可以参考 上面yui的代码 ,不再重述
lastindexof 和 indexof 代码相似 只是从最后开始遍历
下面讲下 ‘ map'
array.prototype.map = function(fun /*, thisp*/) {
var len = this.length >>> 0;
if (typeof fun != function)
throw new typeerror();
var res = new array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
遍历数组,执行函数,迭代数组,每个元素作为参数执行callback方法,由callback方法对每个元素进行处理,最后返回处理后的一个数组
var numbers = [1, 4, 9];
var roots = numbers.map(function(a){return a * 2});
array.prototype.reduce
array.prototype.reduce = function(fun /*, initial*/) {
var len = this.length >>> 0;
if (typeof fun != function)
throw new typeerror();
if (len == 0 && arguments.length == 1)
throw new typeerror();
var i = 0;
if (arguments.length >= 2) {
var rv = arguments[1];
} else {
do {
if (i in this) {
rv = this[i++];
break;
}
if (++i >= len)
throw new typeerror();
} while (true);
}
for (; i < len; i++) {
if (i in this)
rv = fun.call(null, rv, this[i], i, this);
}
return rv;
};
让数组元素依次调用给定函数,最后返回一个值,换言之给定函数一定要用返回值
array.prototype.reduceright
见名故而思意,从右往左
array.prototype.reduceright = function(fun /*, initial*/) {
var len = this.length >>> 0;
if (typeof fun != function)
throw new typeerror();
if (len == 0 && arguments.length == 1)
throw new typeerror();
var i = len - 1;
if (arguments.length >= 2) {
var rv = arguments[1];
} else {
do {
if (i in this) {
rv = this[i--];
break;
}
if (--i < 0)
throw new typeerror();
} while (true);
}
for (; i >= 0; i--) {
if (i in this)
rv = fun.call(null, rv, this[i], i, this);
}
return rv;
};
除了这些,只用想用到的方法都能加到array.prototype上
比如常用的tostring
array.prototype.tostring = function () {
return this.join('');
};
还可以添加 tojson ,uniq ,compact,reverse等
其它类似信息

推荐信息