×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Added: Aug 26, 2019 3:02 PM
Views: 3993
Tags: no tags
  1. // IE8 Fix
  2. if (typeof Array.prototype.forEach !== 'function') {
  3.     Array.prototype.forEach = function(callback){
  4.         for (var i = 0; i < this.length; i++){
  5.             callback.apply(this, [this[i], i, this]);
  6.         }
  7.     };
  8. }
  9.  
  10. if (typeof Array.prototype.indexOf !== 'function') {
  11.     Array.prototype.indexOf = function(elt /*, from*/)
  12.     {
  13.         var len = this.length >>> 0;
  14.  
  15.         var from = Number(arguments[1]) || 0;
  16.         from = (from < 0)
  17.             ? Math.ceil(from)
  18.             : Math.floor(from);
  19.         if (from < 0)
  20.             from += len;
  21.  
  22.         for (; from < len; from++)
  23.         {
  24.             if (from in this &&
  25.                 this[from] === elt)
  26.                 return from;
  27.         }
  28.         return -1;
  29.     };
  30. }
  31.  
  32. if (typeof Array.isArray !== 'function') {
  33.     Array.isArray = function (obj) {
  34.         return Object.prototype.toString.call(obj) === "[object Array]";
  35.     };
  36. }
  37.  
  38. if (typeof Array.prototype.map !== 'function') {
  39.     Array.prototype.map = function (f) {
  40.         var r = [];
  41.         for (var i = 0; i < this.length; i++) {
  42.             r.push(f(this[i]));
  43.         }
  44.         return r
  45.     }
  46. }
  47.  
  48. if(typeof String.prototype.trim !== 'function') {
  49.     String.prototype.trim = function() {
  50.         return this.replace(/^\s+|\s+$/g, '');
  51.     }
  52. }
  53.  
  54. if(typeof Object.keys !== 'function') {
  55.     Object.keys = (function () {
  56.         'use strict';
  57.         var hasOwnProperty = Object.prototype.hasOwnProperty,
  58.             hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
  59.             dontEnums = [
  60.                 'toString',
  61.                 'toLocaleString',
  62.                 'valueOf',
  63.                 'hasOwnProperty',
  64.                 'isPrototypeOf',
  65.                 'propertyIsEnumerable',
  66.                 'constructor'
  67.             ],
  68.             dontEnumsLength = dontEnums.length;
  69.  
  70.         return function (obj) {
  71.             if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
  72.                 throw new TypeError('Object.keys called on non-object');
  73.             }
  74.  
  75.             var result = [], prop, i;
  76.  
  77.             for (prop in obj) {
  78.                 if (hasOwnProperty.call(obj, prop)) {
  79.                     result.push(prop);
  80.                 }
  81.             }
  82.  
  83.             if (hasDontEnumBug) {
  84.                 for (i = 0; i < dontEnumsLength; i++) {
  85.                     if (hasOwnProperty.call(obj, dontEnums[i])) {
  86.                         result.push(dontEnums[i]);
  87.                     }
  88.                 }
  89.             }
  90.             return result;
  91.         };
  92.     }());
  93. }