js 的一些在IE下不支持的函数

  1. // ============   isArray  ===============//              
  2. // isArray  
  3. function isArray(value){  
  4.     return Object.prototype.toString.call(value) == "[object Array]";  
  5. }  
  6. var arr = [1,2,3,4,5];  
  7. alert(isArray(arr)); // IE8 及以下不支持  
  8.   
  9.   
  10. // ============   filter 等  ===============//     
  11. // 数组的一些方法  every(), filter(), forEach(), map(), some()  
  12. // IE8 及以下不支持  
  13. // 解决办法,以filter为例,自己写一个filter  
  14. if (!Array.prototype.filter) {  
  15.     Array.prototype.filter = function(fun /*, thisp*/){  
  16.         var len = this.length;  
  17.         if (typeof fun != "function"){  
  18.             throw new TypeError();  
  19.         }  
  20.         var res = new Array();  
  21.         var thisp = arguments[1];  
  22.         for (var i = 0; i < len; i++){  
  23.             if (i in this){  
  24.                 var val = this[i]; // in case fun mutates this  
  25.                 if (fun.call(thisp, val, i, this)) {  
  26.                     res.push(val);  
  27.                 }  
  28.             }  
  29.         }  
  30.         return res;  
  31.     };  
  32. }  
  33.   
  34. var numbers = [1,2,3,4,5,6];  
  35. var filterResult = numbers.filter(function(item, inde, array){  
  36.     return (item>2);  
  37. });  
  38. alert(filterResult); // 3,4,5,6  
  39.   
  40.   
  41.   
  42. // ============   Date.now()  ===============//   
  43. // Date.now(); IE8及以下不支持,只能自己写一个解决  
  44. if(!Date.now){  
  45.     Date.now = function(){  
  46.         return new Date().valueOf();  
  47.     }  
  48. }  
  49. alert(Date.now());  
  50.   
  51.   
  52.   
  53.   
  54. // ============   stringValue[1]  ===============//  
  55. // 在IE7 及以下版本显示  undefined    
  56. var stringValue = "hello world";  
  57. alert(stringValue[1]);  
  58.   
  59.   
  60.   
  61. // ============   trim()  ===============//  
  62. // 在IE8 及以下版本无效,需要自己写     
  63. String.prototype.trim = function(){  
  64.     return this.replace(/(^s*)(s*$)/g, "");  
  65. };  
  66.   
  67. var stringValue2 = "   hello world  ";  
  68. alert(stringValue2.trim());  
  1. // ============   isArray  ===============//              
  2. // isArray  
  3. function isArray(value){  
  4.     return Object.prototype.toString.call(value) == "[object Array]";  
  5. }  
  6. var arr = [1,2,3,4,5];  
  7. alert(isArray(arr)); // IE8 及以下不支持  
  8.   
  9.   
  10. // ============   filter 等  ===============//     
  11. // 数组的一些方法  every(), filter(), forEach(), map(), some()  
  12. // IE8 及以下不支持  
  13. // 解决办法,以filter为例,自己写一个filter  
  14. if (!Array.prototype.filter) {  
  15.     Array.prototype.filter = function(fun /*, thisp*/){  
  16.         var len = this.length;  
  17.         if (typeof fun != "function"){  
  18.             throw new TypeError();  
  19.         }  
  20.         var res = new Array();  
  21.         var thisp = arguments[1];  
  22.         for (var i = 0; i < len; i++){  
  23.             if (i in this){  
  24.                 var val = this[i]; // in case fun mutates this  
  25.                 if (fun.call(thisp, val, i, this)) {  
  26.                     res.push(val);  
  27.                 }  
  28.             }  
  29.         }  
  30.         return res;  
  31.     };  
  32. }  
  33.   
  34. var numbers = [1,2,3,4,5,6];  
  35. var filterResult = numbers.filter(function(item, inde, array){  
  36.     return (item>2);  
  37. });  
  38. alert(filterResult); // 3,4,5,6  
  39.   
  40.   
  41.   
  42. // ============   Date.now()  ===============//   
  43. // Date.now(); IE8及以下不支持,只能自己写一个解决  
  44. if(!Date.now){  
  45.     Date.now = function(){  
  46.         return new Date().valueOf();  
  47.     }  
  48. }  
  49. alert(Date.now());  
  50.   
  51.   
  52.   
  53.   
  54. // ============   stringValue[1]  ===============//  
  55. // 在IE7 及以下版本显示  undefined    
  56. var stringValue = "hello world";  
  57. alert(stringValue[1]);  
  58.   
  59.   
  60.   
  61. // ============   trim()  ===============//  
  62. // 在IE8 及以下版本无效,需要自己写     
  63. String.prototype.trim = function(){  
  64.     return this.replace(/(^s*)(s*$)/g, "");  
  65. };  
  66.   
  67. var stringValue2 = "   hello world  ";  
  68. alert(stringValue2.trim());  
原文地址:https://www.cnblogs.com/scorpio-qian/p/6143965.html