javascript – array.map功能不支持IE8标准?

前端之家收集整理的这篇文章主要介绍了javascript – array.map功能不支持IE8标准?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我没有IE8,所以我正在IE10内测试IE8.当我切换到文档模式的“IE8标准”时,数组对象的 JavaScript地图函数给出了一个javascript错误:对象不支持属性方法’map’

但是当我切换到文档模式的“标准”时,没有错误.我应该测试哪种模式?

如果IE8不支持map功能,有没有办法模拟?

解决方法

不支持,但 MDN提供了非常接近规格的垫片:
  1. // Production steps of ECMA-262,Edition 5,15.4.4.19
  2. // Reference: http://es5.github.com/#x15.4.4.19
  3. if (!Array.prototype.map) {
  4. Array.prototype.map = function(callback,thisArg) {
  5.  
  6. var T,A,k;
  7.  
  8. if (this == null) {
  9. throw new TypeError(" this is null or not defined");
  10. }
  11.  
  12. // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
  13. var O = Object(this);
  14.  
  15. // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
  16. // 3. Let len be ToUint32(lenValue).
  17. var len = O.length >>> 0;
  18.  
  19. // 4. If IsCallable(callback) is false,throw a TypeError exception.
  20. // See: http://es5.github.com/#x9.11
  21. if (typeof callback !== "function") {
  22. throw new TypeError(callback + " is not a function");
  23. }
  24.  
  25. // 5. If thisArg was supplied,let T be thisArg; else let T be undefined.
  26. if (thisArg) {
  27. T = thisArg;
  28. }
  29.  
  30. // 6. Let A be a new array created as if by the expression new Array(len) where Array is
  31. // the standard built-in constructor with that name and len is the value of len.
  32. A = new Array(len);
  33.  
  34. // 7. Let k be 0
  35. k = 0;
  36.  
  37. // 8. Repeat,while k < len
  38. while(k < len) {
  39.  
  40. var kValue,mappedValue;
  41.  
  42. // a. Let Pk be ToString(k).
  43. // This is implicit for LHS operands of the in operator
  44. // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
  45. // This step can be combined with c
  46. // c. If kPresent is true,then
  47. if (k in O) {
  48.  
  49. // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
  50. kValue = O[ k ];
  51.  
  52. // ii. Let mappedValue be the result of calling the Call internal method of callback
  53. // with T as the this value and argument list containing kValue,k,and O.
  54. mappedValue = callback.call(T,kValue,O);
  55.  
  56. // iii. Call the DefineOwnProperty internal method of A with arguments
  57. // Pk,Property Descriptor {Value: mappedValue,: true,Enumerable: true,Configurable: true},// and false.
  58.  
  59. // In browsers that support Object.defineProperty,use the following:
  60. // Object.defineProperty(A,Pk,{ value: mappedValue,writable: true,enumerable: true,configurable: true });
  61.  
  62. // For best browser support,use the following:
  63. A[ k ] = mappedValue;
  64. }
  65. // d. Increase k by 1.
  66. k++;
  67. }
  68.  
  69. // 9. return A
  70. return A;
  71. };
  72. }

猜你在找的JavaScript相关文章