polyfill.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. if (!Array.prototype.forEach) {
  2. Array.prototype.forEach = function(callback) {
  3. var _arr = this;
  4. var val, j;
  5. for (var i in _arr) {
  6. if (_arr.hasOwnProperty(i)) {
  7. j = parseInt(i);
  8. val = _arr[j];
  9. callback(val, j, _arr);
  10. }
  11. }
  12. }
  13. }
  14. if (!Array.prototype.map) {
  15. Array.prototype.map = function(callback) {
  16. var _arr = [];
  17. this.forEach(function(el, i, arr) {
  18. _arr.push(callback(el, i, arr));
  19. });
  20. return _arr;
  21. }
  22. }
  23. if (!Array.prototype.filter) {
  24. Array.prototype.filter = function(callback) {
  25. var _arr = [];
  26. this.forEach(function(el, i, arr) {
  27. if (callback(el, i, arr)) {
  28. _arr.push(el);
  29. }
  30. });
  31. return _arr;
  32. }
  33. }
  34. if (!Array.prototype.find) {
  35. Array.prototype.find = function(callback) {
  36. var _arr = this;
  37. var val, j;
  38. for (var i in _arr) {
  39. if (_arr.hasOwnProperty(i)) {
  40. j = parseInt(i);
  41. val = _arr[j];
  42. if (callback(val, j, _arr)) {
  43. return val;
  44. }
  45. }
  46. }
  47. return null;
  48. }
  49. }
  50. if (!Array.prototype.findIndex) {
  51. Array.prototype.findIndex = function(callback) {
  52. var _arr = this;
  53. var val, j;
  54. for (var i in _arr) {
  55. if (_arr.hasOwnProperty(i)) {
  56. j = parseInt(i);
  57. val = _arr[j];
  58. if (callback(val, j, _arr)) {
  59. return j;
  60. }
  61. }
  62. }
  63. return -1;
  64. }
  65. }
  66. if (!Array.prototype.reduce) {
  67. Array.prototype.reduce = function(callback) {
  68. var _arr = this,
  69. len = _arr.length,
  70. a = _arr[0],
  71. b = _arr[1];
  72. a = callback(a, b, 0, _arr);
  73. for (var i = 1; i < len - 2; i++) {
  74. if (_arr.hasOwnProperty(i)) {
  75. b = _arr[i + 1];
  76. a = callback(a, b, i, _arr);
  77. }
  78. }
  79. return a;
  80. }
  81. }
  82. if (!Array.prototype.reduceRight) {
  83. Array.prototype.reduceRight = function(callback) {
  84. var _arr = this,
  85. len = _arr.length,
  86. a = _arr[len - 1],
  87. b = _arr[len - 2];
  88. a = callback(a, b, 0, _arr);
  89. for (var i = len - 2; i > 0; i--) {
  90. if (_arr.hasOwnProperty(i)) {
  91. b = _arr[i - 1];
  92. a = callback(a, b, len - i - 1, _arr);
  93. }
  94. }
  95. return a;
  96. }
  97. }
  98. if (!Array.prototype.some) {
  99. Array.prototype.some = function(callback) {
  100. var _arr = this;
  101. var val, j;
  102. for (var i in _arr) {
  103. if (_arr.hasOwnProperty(i)) {
  104. j = parseInt(i);
  105. val = _arr[j];
  106. if (callback(val, j, _arr)) {
  107. return true;
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. }
  114. if (!Array.prototype.every) {
  115. Array.prototype.every = function(callback) {
  116. var _arr = this;
  117. var val, j;
  118. for (var i in _arr) {
  119. if (_arr.hasOwnProperty(i)) {
  120. j = parseInt(i);
  121. val = _arr[j];
  122. if (!callback(val, j, _arr)) {
  123. return false;
  124. }
  125. }
  126. }
  127. return true;
  128. }
  129. }
  130. if (!Array.prototype.indexOf) {
  131. Array.prototype.indexOf = function(el) {
  132. var _arr = this;
  133. var val;
  134. for (var i in _arr) {
  135. val = _arr[i];
  136. if (_arr.hasOwnProperty(i) && val === el) {
  137. return parseInt(i);
  138. }
  139. }
  140. return -1;
  141. }
  142. }
  143. if (!Function.prototype.bind) {
  144. Function.prototype.bind = function() {
  145. var fn = this,
  146. slice = Array.prototype.slice,
  147. presetArgs = slice.call(arguments),
  148. context = presetArgs.shift();
  149. return function() {
  150. return fn.apply(context, presetArgs.concat(slice.call(arguments)));
  151. };
  152. };
  153. }
  154. if (typeof Object.assign != 'function') {
  155. Object.assign = function(target) {
  156. if (target === undefined || target === null) {
  157. throw new TypeError('Cannot convert undefined or null to object');
  158. }
  159. var output = Object(target);
  160. for (var index = 1; index < arguments.length; index++) {
  161. var source = arguments[index];
  162. if (source !== undefined && source !== null) {
  163. for (var nextKey in source) {
  164. if (source.hasOwnProperty(nextKey)) {
  165. output[nextKey] = source[nextKey];
  166. }
  167. }
  168. }
  169. }
  170. return output;
  171. };
  172. }
  173. if (typeof Object.create !== 'function') {
  174. Object.create = function(o) {
  175. function F() {}
  176. F.prototype = o?o:Object;
  177. F.prototype.constructor = F;
  178. return new F();
  179. };
  180. }
  181. if (!Object.keys) {
  182. Object.keys = (function() {
  183. 'use strict';
  184. var hasOwnProperty = Object.prototype.hasOwnProperty,
  185. hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
  186. dontEnums = [
  187. 'toString',
  188. 'toLocaleString',
  189. 'valueOf',
  190. 'hasOwnProperty',
  191. 'isPrototypeOf',
  192. 'propertyIsEnumerable',
  193. 'constructor'
  194. ],
  195. dontEnumsLength = dontEnums.length;
  196. return function(obj) {
  197. if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
  198. throw new TypeError('Object.keys called on non-object');
  199. }
  200. var result = [],
  201. prop,
  202. i;
  203. for (prop in obj) {
  204. if (hasOwnProperty.call(obj, prop)) {
  205. result.push(prop);
  206. }
  207. }
  208. if (hasDontEnumBug) {
  209. for (i = 0; i < dontEnumsLength; i++) {
  210. if (hasOwnProperty.call(obj, dontEnums[i])) {
  211. result.push(dontEnums[i]);
  212. }
  213. }
  214. }
  215. return result;
  216. };
  217. }());
  218. }
  219. ! function(window) {
  220. if (!window.console) {
  221. window.console = {};
  222. }
  223. var con = window.console;
  224. var prop,
  225. method;
  226. var dummy = function() {};
  227. var properties = ['memory'];
  228. var methods = ('assert,clear,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEn' +
  229. 'd,info,log,markTimeline,profile,profiles,profileEnd,show,table,time,timeEnd,time' +
  230. 'line,timelineEnd,timeStamp,trace,warn').split(',');
  231. while (prop = properties.pop())
  232. if (!con[prop])
  233. con[prop] = {};
  234. while (method = methods.pop())
  235. if (!con[method])
  236. con[method] = dummy;
  237. Array.isArray || (Array.isArray = function(arr) {
  238. return Object.prototype.toString.call(arr) == '[object Array]';
  239. });
  240. Object.is || (Object.is = function is(x, y) {
  241. // SameValue algorithm
  242. if (x === y) {
  243. // Steps 1-5, 7-10 Steps 6.b-6.e: +0 != -0 Added the nonzero y check to make
  244. // Flow happy, but it is redundant
  245. return x !== 0 || y !== 0 || 1 / x === 1 / y;
  246. } else {
  247. // Step 6.a: NaN == NaN
  248. return x !== x && y !== y;
  249. }
  250. });
  251. }(window);