keys.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var getNative = require('../internal/getNative'),
  2. isArrayLike = require('../internal/isArrayLike'),
  3. isObject = require('../lang/isObject'),
  4. shimKeys = require('../internal/shimKeys');
  5. /* Native method references for those with the same name as other `lodash` methods. */
  6. var nativeKeys = getNative(Object, 'keys');
  7. /**
  8. * Creates an array of the own enumerable property names of `object`.
  9. *
  10. * **Note:** Non-object values are coerced to objects. See the
  11. * [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys)
  12. * for more details.
  13. *
  14. * @static
  15. * @memberOf _
  16. * @category Object
  17. * @param {Object} object The object to query.
  18. * @returns {Array} Returns the array of property names.
  19. * @example
  20. *
  21. * function Foo() {
  22. * this.a = 1;
  23. * this.b = 2;
  24. * }
  25. *
  26. * Foo.prototype.c = 3;
  27. *
  28. * _.keys(new Foo);
  29. * // => ['a', 'b'] (iteration order is not guaranteed)
  30. *
  31. * _.keys('hi');
  32. * // => ['0', '1']
  33. */
  34. var keys = !nativeKeys ? shimKeys : function(object) {
  35. var Ctor = object == null ? undefined : object.constructor;
  36. if ((typeof Ctor == 'function' && Ctor.prototype === object) ||
  37. (typeof object != 'function' && isArrayLike(object))) {
  38. return shimKeys(object);
  39. }
  40. return isObject(object) ? nativeKeys(object) : [];
  41. };
  42. module.exports = keys;