zoukankan      html  css  js  c++  java
  • 低版本浏览器下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++;
                }
            };
        };
    });
    

      

  • 相关阅读:
    规划
    学习规划
    续约
    每日一记
    每日记录
    《代码大全》第八章 防御式编程
    《代码大全》第七章
    平安夜
    每日一记
    培养良好的生活习惯
  • 原文地址:https://www.cnblogs.com/wangxuehao/p/6732319.html
Copyright © 2011-2022 走看看