让ie以及老版本w3c浏览器 也支持 js1.6的 数组对象的 几个新增方法.
中国IT站 www.chinaitz.com 2009-3-21

  写了简单注释. 具体用法 请参考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;
        }
    }

共2篇1 2 下一页
责任编辑:admin本文仅代表作者观点,与中国IT站立场无关。
收藏】 【推荐】 【投稿】 【 】 【打印】 【关闭
评论加载中...