• 让forEach兼容IE8


    今天在做项目中遇到一个问题,就是forEach 不兼容IE8;后经查阅资料完美解决问题。

    资料:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach?v=example

     1 //官网查询的兼容代码段
     2 // Production steps of ECMA-262, Edition 5, 15.4.4.18
     3 // Reference: http://es5.github.io/#x15.4.4.18
     4 if (!Array.prototype.forEach) {
     5 
     6   Array.prototype.forEach = function(callback/*, thisArg*/) {
     7 
     8     var T, k;
     9 
    10     if (this == null) {
    11       throw new TypeError('this is null or not defined');
    12     }
    13 
    14     // 1. Let O be the result of calling toObject() passing the
    15     // |this| value as the argument.
    16     var O = Object(this);
    17 
    18     // 2. Let lenValue be the result of calling the Get() internal
    19     // method of O with the argument "length".
    20     // 3. Let len be toUint32(lenValue).
    21     var len = O.length >>> 0;
    22 
    23     // 4. If isCallable(callback) is false, throw a TypeError exception. 
    24     // See: http://es5.github.com/#x9.11
    25     if (typeof callback !== 'function') {
    26       throw new TypeError(callback + ' is not a function');
    27     }
    28 
    29     // 5. If thisArg was supplied, let T be thisArg; else let
    30     // T be undefined.
    31     if (arguments.length > 1) {
    32       T = arguments[1];
    33     }
    34 
    35     // 6. Let k be 0.
    36     k = 0;
    37 
    38     // 7. Repeat while k < len.
    39     while (k < len) {
    40 
    41       var kValue;
    42 
    43       // a. Let Pk be ToString(k).
    44       //    This is implicit for LHS operands of the in operator.
    45       // b. Let kPresent be the result of calling the HasProperty
    46       //    internal method of O with argument Pk.
    47       //    This step can be combined with c.
    48       // c. If kPresent is true, then
    49       if (k in O) {
    50 
    51         // i. Let kValue be the result of calling the Get internal
    52         // method of O with argument Pk.
    53         kValue = O[k];
    54 
    55         // ii. Call the Call internal method of callback with T as
    56         // the this value and argument list containing kValue, k, and O.
    57         callback.call(T, kValue, k, O);
    58       }
    59       // d. Increase k by 1.
    60       k++;
    61     }
    62     // 8. return undefined.
    63   };
    64 }
    65 //此处放兼容代码
    66 /*兼容代码*/
    67 //resultArr数组
    68 resultArr.forEach(function (iterm, index) {
    69       //内容                 
    70 })
    什么叫见过大世面?就是能享受最好的,能承受最坏的。
  • 相关阅读:
    51nod 1179 最大的最大公约数
    HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5
    HDU 6073 Matching In Multiplication —— 2017 Multi-University Training 4
    HDU 6069 Counting Divisors —— 2017 Multi-University Training 4
    HDU 6058 Kanade's sum —— 2017 Multi-University Training 3
    HDU 6055 Regular polygon —— 2017 Multi-University Training 2
    HDU 6053 TrickGCD —— 2017 Multi-University Training 2
    ORA-01722:无效数字
    ORA-01427 单行子查询返回多个行
    ORA 00972 错误处理
  • 原文地址:https://www.cnblogs.com/jingaier/p/7064210.html
Copyright © 2020-2023  润新知