_polyfill.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. if (!Array.prototype.forEach) {
  2. Array.prototype.forEach = function(callback, thisArg) {
  3. var T, k;
  4. if (this == null) {
  5. throw new TypeError(' this is null or not defined');
  6. }
  7. // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
  8. var O = Object(this);
  9. // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
  10. // 3. Let len be ToUint32(lenValue).
  11. var len = O.length >>> 0;
  12. // 4. If IsCallable(callback) is false, throw a TypeError exception.
  13. // See: http://es5.github.com/#x9.11
  14. if (typeof callback !== "function") {
  15. throw new TypeError(callback + ' is not a function');
  16. }
  17. // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
  18. if (arguments.length > 1) {
  19. T = thisArg;
  20. }
  21. // 6. Let k be 0
  22. k = 0;
  23. // 7. Repeat, while k < len
  24. while (k < len) {
  25. var kValue;
  26. // a. Let Pk be ToString(k).
  27. // This is implicit for LHS operands of the in operator
  28. // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
  29. // This step can be combined with c
  30. // c. If kPresent is true, then
  31. if (k in O) {
  32. // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
  33. kValue = O[k];
  34. // ii. Call the Call internal method of callback with T as the this value and
  35. // argument list containing kValue, k, and O.
  36. callback.call(T, kValue, k, O);
  37. }
  38. // d. Increase k by 1.
  39. k++;
  40. }
  41. // 8. return undefined
  42. };
  43. }
  44. if (!Array.isArray) {
  45. Array.isArray = function(arg) {
  46. return Object.prototype.toString.call(arg) === '[object Array]';
  47. };
  48. }
  49. if (!Array.prototype.map) {
  50. Array.prototype.map = function(callback, thisArg) {
  51. var T, A, k;
  52. if (this == null) {
  53. throw new TypeError(' this is null or not defined');
  54. }
  55. // 1. Let O be the result of calling ToObject passing the |this|
  56. // value as the argument.
  57. var O = Object(this);
  58. // 2. Let lenValue be the result of calling the Get internal
  59. // method of O with the argument "length".
  60. // 3. Let len be ToUint32(lenValue).
  61. var len = O.length >>> 0;
  62. // 4. If IsCallable(callback) is false, throw a TypeError exception.
  63. // See: http://es5.github.com/#x9.11
  64. if (typeof callback !== 'function') {
  65. throw new TypeError(callback + ' is not a function');
  66. }
  67. // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
  68. if (arguments.length > 1) {
  69. T = thisArg;
  70. }
  71. // 6. Let A be a new array created as if by the expression new Array(len)
  72. // where Array is the standard built-in constructor with that name and
  73. // len is the value of len.
  74. A = new Array(len);
  75. // 7. Let k be 0
  76. k = 0;
  77. // 8. Repeat, while k < len
  78. while (k < len) {
  79. var kValue, mappedValue;
  80. // a. Let Pk be ToString(k).
  81. // This is implicit for LHS operands of the in operator
  82. // b. Let kPresent be the result of calling the HasProperty internal
  83. // method of O with argument Pk.
  84. // This step can be combined with c
  85. // c. If kPresent is true, then
  86. if (k in O) {
  87. // i. Let kValue be the result of calling the Get internal
  88. // method of O with argument Pk.
  89. kValue = O[k];
  90. // ii. Let mappedValue be the result of calling the Call internal
  91. // method of callback with T as the this value and argument
  92. // list containing kValue, k, and O.
  93. mappedValue = callback.call(T, kValue, k, O);
  94. // iii. Call the DefineOwnProperty internal method of A with arguments
  95. // Pk, Property Descriptor
  96. // { Value: mappedValue,
  97. // Writable: true,
  98. // Enumerable: true,
  99. // Configurable: true },
  100. // and false.
  101. // In browsers that support Object.defineProperty, use the following:
  102. // Object.defineProperty(A, k, {
  103. // value: mappedValue,
  104. // writable: true,
  105. // enumerable: true,
  106. // configurable: true
  107. // });
  108. // For best browser support, use the following:
  109. A[k] = mappedValue;
  110. }
  111. // d. Increase k by 1.
  112. k++;
  113. }
  114. // 9. return A
  115. return A;
  116. };
  117. }