polyfill.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
  2. if (!Object.keys) {
  3. Object.keys = (function () {
  4. 'use strict';
  5. var hasOwnProperty = Object.prototype.hasOwnProperty,
  6. hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
  7. dontEnums = [
  8. 'toString',
  9. 'toLocaleString',
  10. 'valueOf',
  11. 'hasOwnProperty',
  12. 'isPrototypeOf',
  13. 'propertyIsEnumerable',
  14. 'constructor'
  15. ],
  16. dontEnumsLength = dontEnums.length;
  17. return function (obj) {
  18. if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
  19. throw new TypeError('Object.keys called on non-object');
  20. }
  21. var result = [], prop, i;
  22. for (prop in obj) {
  23. if (hasOwnProperty.call(obj, prop)) {
  24. result.push(prop);
  25. }
  26. }
  27. if (hasDontEnumBug) {
  28. for (i = 0; i < dontEnumsLength; i++) {
  29. if (hasOwnProperty.call(obj, dontEnums[i])) {
  30. result.push(dontEnums[i]);
  31. }
  32. }
  33. }
  34. return result;
  35. };
  36. }());
  37. }
  38. //Production steps of ECMA-262, Edition 5, 15.4.4.18
  39. //Reference: http://es5.github.com/#x15.4.4.18
  40. if (!Array.prototype.forEach) {
  41. Array.prototype.forEach = function (callback, thisArg) {
  42. var T, k;
  43. if (this == null) {
  44. throw new TypeError(' this is null or not defined');
  45. }
  46. // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
  47. var O = Object(this);
  48. // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
  49. // 3. Let len be ToUint32(lenValue).
  50. var len = O.length >>> 0;
  51. // 4. If IsCallable(callback) is false, throw a TypeError exception.
  52. // See: http://es5.github.com/#x9.11
  53. if (typeof callback !== "function") {
  54. throw new TypeError(callback + " is not a function");
  55. }
  56. // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
  57. if (arguments.length > 1) {
  58. T = thisArg;
  59. }
  60. // 6. Let k be 0
  61. k = 0;
  62. // 7. Repeat, while k < len
  63. while (k < len) {
  64. var kValue;
  65. // a. Let Pk be ToString(k).
  66. // This is implicit for LHS operands of the in operator
  67. // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
  68. // This step can be combined with c
  69. // c. If kPresent is true, then
  70. if (k in O) {
  71. // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
  72. kValue = O[k];
  73. // ii. Call the Call internal method of callback with T as the this value and
  74. // argument list containing kValue, k, and O.
  75. callback.call(T, kValue, k, O);
  76. }
  77. // d. Increase k by 1.
  78. k++;
  79. }
  80. // 8. return undefined
  81. };
  82. }