contra.shim.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. (function (Object, Array) {
  2. 'use strict';
  3. if (!Array.prototype.forEach) {
  4. Array.prototype.forEach = function (fn, ctx) {
  5. if (this === void 0 || this === null || typeof fn !== 'function') {
  6. throw new TypeError();
  7. }
  8. var t = this;
  9. var len = t.length;
  10. for (var i = 0; i < len; i++) {
  11. if (i in t) { fn.call(ctx, t[i], i, t); }
  12. }
  13. };
  14. }
  15. if (!Array.prototype.indexOf) {
  16. Array.prototype.indexOf = function (what, start) {
  17. if (this === undefined || this === null) {
  18. throw new TypeError();
  19. }
  20. var length = this.length;
  21. start = +start || 0;
  22. if (Math.abs(start) === Infinity) {
  23. start = 0;
  24. } else if (start < 0) {
  25. start += length;
  26. if (start < 0) { start = 0; }
  27. }
  28. for (; start < length; start++) {
  29. if (this[start] === what) {
  30. return start;
  31. }
  32. }
  33. return -1;
  34. };
  35. }
  36. if (!Array.prototype.filter) {
  37. Array.prototype.filter = function (fn, ctx) {
  38. var f = [];
  39. this.forEach(function (v, i, t) {
  40. if (fn.call(ctx, v, i, t)) { f.push(v); }
  41. }, ctx);
  42. return f;
  43. };
  44. }
  45. if (!Function.prototype.bind) {
  46. Function.prototype.bind = function (context) {
  47. if (typeof this !== 'function') {
  48. throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
  49. }
  50. var curried = Array.prototype.slice.call(arguments, 1);
  51. var original = this;
  52. var NoOp = function () {};
  53. var bound = function () {
  54. var ctx = this instanceof NoOp && context ? this : context;
  55. var args = curried.concat(Array.prototype.slice.call(arguments));
  56. return original.apply(ctx, args);
  57. };
  58. NoOp.prototype = this.prototype;
  59. bound.prototype = new NoOp();
  60. return bound;
  61. };
  62. }
  63. if (!Object.keys) {
  64. Object.keys = function (o) {
  65. var keys = [];
  66. for (var k in o) {
  67. if (o.hasOwnProperty(k)) {
  68. keys.push(k);
  69. }
  70. }
  71. return keys;
  72. };
  73. }
  74. if (Object.defineProperty) { // test for IE8 partial implementation
  75. try { Object.defineProperty({}, 'x', {}); } catch (e) { Object.definePropertyPartial = true; }
  76. }
  77. })(Object, Array);