写了一个数组扩展的方法,但是不能弹出结果,请大虾们指教://动态改变数组中每个元素的值
Array.prototype._filter = Array.prototype.filter ||
(Array.prototype.filter = function () { //数组元素批处理方法
var b = arguments, a = []; //获取参数,并定义一个临时数组
this.each(function () { //调用迭代器,遍历所有元素
a.push(b[0].call(b[1], this)); //调用参数函数,把当前元素作为参数传递,执行返回值存储在临时数组中
});
return a; //返回临时数组
});
Object.prototype.filter = Array.prototype._filter; var a = [1, 2, 3, 4,5,6,7,8,9]; //定义数组直接量
var f = function (x) {
//定义一个元素处理函数,该函数将把每个元素取平方值
if(x>4) return true;
}
var b = a.filter(f); //调用数组元素编辑方法edit(),并传递将要执行的函数
alert(b)
Array.prototype._filter = Array.prototype.filter ||
(Array.prototype.filter = function () { //数组元素批处理方法
var b = arguments, a = []; //获取参数,并定义一个临时数组
this.each(function () { //调用迭代器,遍历所有元素
a.push(b[0].call(b[1], this)); //调用参数函数,把当前元素作为参数传递,执行返回值存储在临时数组中
});
return a; //返回临时数组
});
Object.prototype.filter = Array.prototype._filter; var a = [1, 2, 3, 4,5,6,7,8,9]; //定义数组直接量
var f = function (x) {
//定义一个元素处理函数,该函数将把每个元素取平方值
if(x>4) return true;
}
var b = a.filter(f); //调用数组元素编辑方法edit(),并传递将要执行的函数
alert(b)
try {
this.i || (this.i = 0);
if (this.length > 0 && f.constructor == Function) {
while (this.i < this.length) {
var e = this[this.i];
if (e && e.constructor == Array) {
e.each(fff);
}
else {
f.apply(e, [e]);
}
this.i++;
}
this.i = null;
}
}
catch (w) { }
return this;
}