让ie以及老版本w3c浏览器 也支持 js1.6的 数组对象的 几个新增方法.
2010-09-14 13:41:49 来源:WEB开发网写了简单注释. 具体用法 请参考js1.6手册 .
// 模拟js1.6 Array.prototype.forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(f, oThis) {
if (!f || f.constructor != Function.toString()) return;
oThis = oThis || window;
for (var i = 0, len = this.length; i < len; i++) {
f.call(oThis, this[i], i, this); //p1 上下文环境 p2 数组元素 p3 索引 p4 数组对象
}
}
}
//模拟js1.6 Array.prototype.filter
if (!Array.prototype.filter) {
Array.prototype.filter = function(f, oThis) {
if (!f || f.constructor != Function.toString()) return;
oThis = oThis || window;
var a = [];
for (var i = 0, len = this.length; i < len; i++) {
if (f.call(oThis, this[i], i, this)) a.push(this[i]);
}
return a;
}
}
//模拟js1.6 Array.prototype.map
if (!Array.prototype.map) {
Array.prototype.map = function(f, oThis) {
if (!f || f.constructor != Function.toString()) return;
oThis = oThis || window;
var a = [];
for (var i = 0, len = this.length; i < len; i++) {
a.push(f.call(oThis, this[i], i, this));
}
return a;
}
}
//模拟 js1.6 Array.prototype.every
if (!Array.prototype.every) {
Array.prototype.every = function(f, oThis) {
if (!f || f.constructor != Function.toString()) return;
oThis = oThis || window;
for (var i = 0, len = this.length; i < len; i++) {
if (!f.call(oThis, this[i], i, this)) return false;
}
return true;
}
}
//模拟 js1.6 Array.prototype.some
if (!Array.prototype.some) {
Array.prototype.some = function(f, oThis) {
if (!f || f.constructor != Function.toString()) return;
oThis = oThis || window;
for (var i = 0, len = this.length; i < len; i++) {
if (f.call(oThis, this[i], i, this)) return true;
}
return false;
}
}
更多精彩
赞助商链接