低版本浏览器下Array之forEach

 

低版本浏览器不支持Array的forEach,解决办法

define(function(require,exports,module){
    /**
     * Invokes given callback function for each element of an array.
     * ECMAScript 5 Reference: 15.4.4.18
     * @param {function} callback a callback
     * @throws {TypeError} when callback is not callable object
     * @example [1,2,3].forEach(function(el){ console.log(el); });
     */
    if (!Array.prototype.forEach){
        Array.prototype.forEach = function(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 ({}.toString.call(callback) != "[object Function]"){
                throw new TypeError(callback + " is not a function");
            }
            if (thisArg){
                T = thisArg;
            }
            k = 0;
            while(k < len){
                var kValue;
                if (k in O){
                    kValue = O[k];
                    callback.call(T, kValue, k, O);
                }
                k++;
            }
        };
    };
});

  

posted on 2017-04-19 11:05  王雪皓  阅读(232)  评论(0编辑  收藏  举报