Object.keys VS Object.getOwnPropertyNames

Asen Bozhilov asen.bozhilov at gmail.com
Sun Apr 18 08:15:19 PDT 2010


Jürg Lehni wrote:
> I am aware that these function names are probably set in stone by now, but am curious to hear the reasons for their naming, as there seem to be many discrepancies in there, mostly:
>
> - The use of both 'key' and 'name' for the same thing
> - The omitting of 'get' in Object.keys
>
> In terms of a clean API, something along these lines would have seemed more consistent:
>
> Object.getKeys
> Object.getOwnKeys
>   

Your suggestions are not quite clear, because Object.keys and 
Object.getOwnPropertyNames do different things. getOwnPropertyNames 
include properties which have [[Enumerable]] attribute equal to false. 
In other words include properties which are not enumerable. Object.keys 
include only enumerable properties, for which [[Enumerable]] is equal to 
true.

In nature of ES with method as Object.keys I expect property names, 
which are enumerable in whole prototype chain of passed object. With 
Object.keys I do not see any benefits. Again If there is:

for own(var i in obj) {
}

Methods like those are not necessary.  Or another  approach is :

Object.prototype.forOwn = function(callback, thisValue) {
    /**
     * For next own property:
     * callback.call(thisValue, this[i], i, this);
     */
};


At the moment I need to get first array with properties names and second 
iterate over that array to do something with every property name. That 
is not improvements in the language. However regarding the names, you 
are correct they are not clear. For example:

Object.getOwnKeys  ->  Object.getOwnPropertyNames
Object.getOwnEnumKeys  -> Object.keys


Is ES5 get idea for Object.keys by JS libraries, which use that method? 
For example Prototype.js?


More information about the es-discuss mailing list