美文网首页
IE8兼容 indexOf和forEach

IE8兼容 indexOf和forEach

作者: 爷爷的毛丫头 | 来源:发表于2019-01-14 11:17 被阅读0次

1. indexOf兼容IE8

if (!Array.prototype.indexOf) {

    Array.prototype.indexOf = function(elt

    /*, from*/

    ) {

        var len = this.length >>> 0;

        var from = Number(arguments[1]) || 0;

        from = (from < 0) ? Math.ceil(from) : Math.floor(from);

        if (from < 0) from += len;

        for (; from < len; from++) {

            if (from in this && this[from] === elt) return from;

        }

        return - 1;

    };

}

2.forEach兼容IE8

if (!Array.prototype.forEach) {

    Array.prototype.forEach = function forEach(callback, thisArg) {

        var T, k;

        if (this == null) {

            throw new TypeError("this is null or not defined");

        }

        var O = Object(this);

        var len = O.length >>> 0;

        if (typeof callback !== "function") {

            throw new TypeError(callback + " is not a function");

        }

        if (arguments.length > 1) {

            T = thisArg;

        }

        k = 0;

        while (k < len) {

            var kValue;

            if (k in O) {

                kValue = O[k];

                callback.call(T, kValue, k, O);

            }

            k++;

        }

    };

}

相关文章

网友评论

      本文标题:IE8兼容 indexOf和forEach

      本文链接:https://www.haomeiwen.com/subject/ygyurqtx.html