object_keys.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. }