让ie以及老版本w3c浏览器 也支持ES5的 数组对象的 几个新增方法.

写了简单注释. 具体用法 请参考 ES5 手册 .

// 模拟ES5 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 数组对象
            }
        }
    }

    //模拟 ES5 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;
        }
    }

    //模拟 ES5 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;
        }
    }

    //模拟 ES5 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;
        }
    }

    //模拟 ES5 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;
        }
    }

 

要重点说一说的 indexOf lastIndexOf 两个方法 .. 我只是实现了一个简单版本.但修复了 一点点小问题 . 先看代码

//模拟 ES5 Array.prototype.indexOf方法.并修复ff等其他实现 indexOf方法的浏览器中值类型于引用类型比较相等性一律返回false问题
    Array.prototype.indexOf = function(obj) {
        for (var i = 0, len = this.length; i < len; i++) {
            if (compare(this[i], obj)) return i;
        }
        return -1;
    }
    //模拟 ES5 Array.prototype.lastIndexOf方法.并修复ff等其他实现 indexOf方法的浏览器中值类型于引用类型比较相等性一律返回false问题
    Array.prototype.lastIndexOf = function(obj) {
        for (var i = this.length - 1; i >= 0; i--) {
            if (compare(this[i], obj)) return i;
        }
        return -1;
    }

是的. 注释已经写的明白.  我这里之所以 用这个方法 覆盖了 ES5 的方法原因在于


    //比较对象是否于参数obj 相等..
    function compare(obj1, obj2) {
        if (obj1 == null || obj2 == null) return (obj1 === obj2);
        return (obj1 == obj2 && obj1.constructor.toString() == obj2.constructor);
    }

这个 compare方法 . 他解决了一个什么问题呢? 对了! 就是相等性判断. 因为我发现  原始版本的相等性判断 居然会认为

1!=new Number(1)  即 假如数组中存在一个 number对象 而我用 number 直接量去做比较 即使本应该相等. 也会返回false

这于 javascript  引用类型 于值类型做 相等性运算时  会调用引用类型 即对象的 valueOf方法 然后再去做比较 是相违背的.

说明 javascript 1.6 在实现 indexOf方法时 相等性判断 他简单的用了 === 即严格相等判断 ...

那么我写的compare 方法 的作用即 解决这个问题. 让相等性判断 遵循 javascript的原始规则...

说到这里 不得不提一下  老外 在判断 某个对象 为某特定类型时. 会使用 Object.porototyp.toString.call(this) 来做比较. 目的是为了防止

如 [1,2,3].constructor!=iframe.contentWindow.Array 这类情况...

其实大可不必那么麻烦 .  我们只需要调用 两边的构造器对象 的其中一个的toString方法 返回一个 值类型的 string 伪对象即可.

这样 另外一边的引用类型 也会 自动调用 valueOf方法 于字符串做比较.. 何必 搞的那么麻烦呢?

 

最后. 恩 总体来说 我非常喜欢 forEach  map filter 等等方法 . 好用的很. 由于其他几个方法 都是类似的. 所以 一并写出来了! 

posted @ 2009-03-11 22:03  Franky  阅读(3100)  评论(11编辑  收藏  举报