getOwnPropertyNames

Introduced in JavaScript 1.8.5

Summary

Returns an array of all properties (enumerable or not) found upon a given object.

Method of Object
Implemented in JavaScript 1.8.5
ECMAScript Edition ECMAScript 5th Edition

Syntax

Object.getOwnPropertyNames(obj)

Parameters

obj
The object whose enumerable and non-enumerable own properties are to be returned.

Description

Object.getOwnPropertyNames returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly upon obj. The ordering of the enumerable properties in the array is consistent with the ordering exposed by a  for...in loop (or by Object.keys) over the properties of the object.  The ordering of the non-enumerable properties in the array, and among the enumerable properties, is not defined.

Examples

var arr = ["a", "b", "c"];
print(Object.getOwnPropertyNames(arr).sort()); // prints "0,1,2,length"

// array-like object
var obj = { 0: "a", 1: "b", 2: "c"};
print(Object.getOwnPropertyNames(obj).sort()); // prints "0,1,2"

// non-enumerable property
var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; }, enumerable: false } });
my_obj.foo = 1;

print(Object.getOwnPropertyNames(my_obj).sort()); // prints "foo, getFoo"

If you want only the enumerable properties, see Object.keys or use a for...in loop (although note that this will return enumerable properties not found directly upon that object but also along the prototype chain for the object).

Cross-browser compatibility

Based on Kangax's compat table.

  • Desktop
  • Mobile

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 4 (2.0) 5 9 -- 5
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ?

See also

Tags (3)

Edit tags

Attachments (0)

 

Attach file