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