You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ie8-polyfill.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // These polyfills taken from MDN (developer.mozilla.org)
  2. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
  3. if (!Object.keys) {
  4. Object.keys = (function() {
  5. 'use strict';
  6. var hasOwnProperty = Object.prototype.hasOwnProperty,
  7. hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
  8. dontEnums = [
  9. 'toString',
  10. 'toLocaleString',
  11. 'valueOf',
  12. 'hasOwnProperty',
  13. 'isPrototypeOf',
  14. 'propertyIsEnumerable',
  15. 'constructor'
  16. ],
  17. dontEnumsLength = dontEnums.length;
  18. return function(obj) {
  19. if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
  20. throw new TypeError('Object.keys called on non-object');
  21. }
  22. var result = [], prop, i;
  23. for (prop in obj) {
  24. if (hasOwnProperty.call(obj, prop)) {
  25. result.push(prop);
  26. }
  27. }
  28. if (hasDontEnumBug) {
  29. for (i = 0; i < dontEnumsLength; i++) {
  30. if (hasOwnProperty.call(obj, dontEnums[i])) {
  31. result.push(dontEnums[i]);
  32. }
  33. }
  34. }
  35. return result;
  36. };
  37. }());
  38. }
  39. // Production steps of ECMA-262, Edition 5, 15.4.4.18
  40. // Reference: http://es5.github.io/#x15.4.4.18
  41. if (!Array.prototype.forEach) {
  42. Array.prototype.forEach = function(callback, thisArg) {
  43. var T, k;
  44. if (this == null) {
  45. throw new TypeError(' this is null or not defined');
  46. }
  47. // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
  48. var O = Object(this);
  49. // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
  50. // 3. Let len be ToUint32(lenValue).
  51. var len = O.length >>> 0;
  52. // 4. If IsCallable(callback) is false, throw a TypeError exception.
  53. // See: http://es5.github.com/#x9.11
  54. if (typeof callback !== "function") {
  55. throw new TypeError(callback + ' is not a function');
  56. }
  57. // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
  58. if (arguments.length > 1) {
  59. T = thisArg;
  60. }
  61. // 6. Let k be 0
  62. k = 0;
  63. // 7. Repeat, while k < len
  64. while (k < len) {
  65. var kValue;
  66. // a. Let Pk be ToString(k).
  67. // This is implicit for LHS operands of the in operator
  68. // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
  69. // This step can be combined with c
  70. // c. If kPresent is true, then
  71. if (k in O) {
  72. // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
  73. kValue = O[k];
  74. // ii. Call the Call internal method of callback with T as the this value and
  75. // argument list containing kValue, k, and O.
  76. callback.call(T, kValue, k, O);
  77. }
  78. // d. Increase k by 1.
  79. k++;
  80. }
  81. // 8. return undefined
  82. };
  83. }
  84. // Production steps of ECMA-262, Edition 5, 15.4.4.14
  85. // Reference: http://es5.github.io/#x15.4.4.14
  86. if (!Array.prototype.indexOf) {
  87. Array.prototype.indexOf = function(searchElement, fromIndex) {
  88. var k;
  89. // 1. Let O be the result of calling ToObject passing
  90. // the this value as the argument.
  91. if (this == null) {
  92. throw new TypeError('"this" is null or not defined');
  93. }
  94. var O = Object(this);
  95. // 2. Let lenValue be the result of calling the Get
  96. // internal method of O with the argument "length".
  97. // 3. Let len be ToUint32(lenValue).
  98. var len = O.length >>> 0;
  99. // 4. If len is 0, return -1.
  100. if (len === 0) {
  101. return -1;
  102. }
  103. // 5. If argument fromIndex was passed let n be
  104. // ToInteger(fromIndex); else let n be 0.
  105. var n = +fromIndex || 0;
  106. if (Math.abs(n) === Infinity) {
  107. n = 0;
  108. }
  109. // 6. If n >= len, return -1.
  110. if (n >= len) {
  111. return -1;
  112. }
  113. // 7. If n >= 0, then Let k be n.
  114. // 8. Else, n<0, Let k be len - abs(n).
  115. // If k is less than 0, then let k be 0.
  116. k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
  117. // 9. Repeat, while k < len
  118. while (k < len) {
  119. // a. Let Pk be ToString(k).
  120. // This is implicit for LHS operands of the in operator
  121. // b. Let kPresent be the result of calling the
  122. // HasProperty internal method of O with argument Pk.
  123. // This step can be combined with c
  124. // c. If kPresent is true, then
  125. // i. Let elementK be the result of calling the Get
  126. // internal method of O with the argument ToString(k).
  127. // ii. Let same be the result of applying the
  128. // Strict Equality Comparison Algorithm to
  129. // searchElement and elementK.
  130. // iii. If same is true, return k.
  131. if (k in O && O[k] === searchElement) {
  132. return k;
  133. }
  134. k++;
  135. }
  136. return -1;
  137. };
  138. }