es5.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // https://developer.mozilla.org/en/JavaScript/Reference/global_objects/array/foreach
  2. if (!Array.prototype.forEach)
  3. {
  4. Array.prototype.forEach = function(fun /*, thisp */)
  5. {
  6. "use strict";
  7. if (this === void 0 || this === null)
  8. throw new TypeError();
  9. var t = Object(this);
  10. var len = t.length >>> 0;
  11. if (typeof fun !== "function")
  12. throw new TypeError();
  13. var thisp = arguments[1];
  14. for (var i = 0; i < len; i++)
  15. {
  16. if (i in t)
  17. fun.call(thisp, t[i], i, t);
  18. }
  19. };
  20. }
  21. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
  22. // Production steps of ECMA-262, Edition 5, 15.4.4.19
  23. // Reference: http://es5.github.com/#x15.4.4.19
  24. if (!Array.prototype.map) {
  25. Array.prototype.map = function(callback, thisArg) {
  26. var T, A, k;
  27. if (this == null) {
  28. throw new TypeError(" this is null or not defined");
  29. }
  30. // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
  31. var O = Object(this);
  32. // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
  33. // 3. Let len be ToUint32(lenValue).
  34. var len = O.length >>> 0;
  35. // 4. If IsCallable(callback) is false, throw a TypeError exception.
  36. // See: http://es5.github.com/#x9.11
  37. if ({}.toString.call(callback) != "[object Function]") {
  38. throw new TypeError(callback + " is not a function");
  39. }
  40. // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
  41. if (thisArg) {
  42. T = thisArg;
  43. }
  44. // 6. Let A be a new array created as if by the expression new Array(len) where Array is
  45. // the standard built-in constructor with that name and len is the value of len.
  46. A = new Array(len);
  47. // 7. Let k be 0
  48. k = 0;
  49. // 8. Repeat, while k < len
  50. while(k < len) {
  51. var kValue, mappedValue;
  52. // a. Let Pk be ToString(k).
  53. // This is implicit for LHS operands of the in operator
  54. // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
  55. // This step can be combined with c
  56. // c. If kPresent is true, then
  57. if (k in O) {
  58. // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
  59. kValue = O[ k ];
  60. // ii. Let mappedValue be the result of calling the Call internal method of callback
  61. // with T as the this value and argument list containing kValue, k, and O.
  62. mappedValue = callback.call(T, kValue, k, O);
  63. // iii. Call the DefineOwnProperty internal method of A with arguments
  64. // Pk, Property Descriptor {Value: mappedValue, Writable: true, Enumerable: true, Configurable: true},
  65. // and false.
  66. // In browsers that support Object.defineProperty, use the following:
  67. // Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true });
  68. // For best browser support, use the following:
  69. A[ k ] = mappedValue;
  70. }
  71. // d. Increase k by 1.
  72. k++;
  73. }
  74. // 9. return A
  75. return A;
  76. };
  77. }
  78. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
  79. if (!Array.prototype.reduce)
  80. {
  81. Array.prototype.reduce = function(fun /*, initialValue */)
  82. {
  83. "use strict";
  84. if (this === void 0 || this === null)
  85. throw new TypeError();
  86. var t = Object(this);
  87. var len = t.length >>> 0;
  88. if (typeof fun !== "function")
  89. throw new TypeError();
  90. // no value to return if no initial value and an empty array
  91. if (len == 0 && arguments.length == 1)
  92. throw new TypeError();
  93. var k = 0;
  94. var accumulator;
  95. if (arguments.length >= 2)
  96. {
  97. accumulator = arguments[1];
  98. }
  99. else
  100. {
  101. do
  102. {
  103. if (k in t)
  104. {
  105. accumulator = t[k++];
  106. break;
  107. }
  108. // if array contains no values, no initial value to return
  109. if (++k >= len)
  110. throw new TypeError();
  111. }
  112. while (true);
  113. }
  114. while (k < len)
  115. {
  116. if (k in t)
  117. accumulator = fun.call(undefined, accumulator, t[k], k, t);
  118. k++;
  119. }
  120. return accumulator;
  121. };
  122. }
  123. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
  124. if (!Array.prototype.indexOf) {
  125. Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
  126. "use strict";
  127. if (this === void 0 || this === null) {
  128. throw new TypeError();
  129. }
  130. var t = Object(this);
  131. var len = t.length >>> 0;
  132. if (len === 0) {
  133. return -1;
  134. }
  135. var n = 0;
  136. if (arguments.length > 0) {
  137. n = Number(arguments[1]);
  138. if (n !== n) { // shortcut for verifying if it's NaN
  139. n = 0;
  140. } else if (n !== 0 && n !== Infinity && n !== -Infinity) {
  141. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  142. }
  143. }
  144. if (n >= len) {
  145. return -1;
  146. }
  147. var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
  148. for (; k < len; k++) {
  149. if (k in t && t[k] === searchElement) {
  150. return k;
  151. }
  152. }
  153. return -1;
  154. }
  155. }