Object.getOwnPropertyNames()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
  • Revision title: getOwnPropertyNames
  • Revision id: 16129
  • Created:
  • Creator: JamesWatts
  • Is current revision? No
  • Comment 5 words added

Revision Content

{{ js_minversion_header("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).

See also

Revision Source

<p>{{ js_minversion_header("1.8.5") }}</p>
<h3 name="Summary">Summary</h3>
<p>Returns an array of all properties (enumerable or not) found upon a given object.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.8.5</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<p><code>Object.getOwnPropertyNames(<em>obj</em>)</code></p>
<h3 name="Parameters">Parameters</h3>
<dl> <dt>obj</dt> <dd>The object whose enumerable <em>and non-enumerable</em> own properties are to be returned.</dd>
</dl>
<h3 name="Description">Description</h3>
<p><code>Object.</code>getOwnPropertyNames returns an array whose elements are strings corresponding to the enumerable <em>and non-enumerable</em> properties found directly upon <code>obj</code>. The ordering of the enumerable properties in the array is consistent with the ordering exposed by a  <a href="/en/JavaScript/Reference/Statements/for...in" style="text-decoration: none; color: rgb(4, 137, 183) !important; cursor: default; " title="en/Core JavaScript 1.5 Reference/Statements/for...in">for...in loop</a> (or by <a href="/en/JavaScript/Reference/Global_Objects/Object/keys" style="text-decoration: none; color: rgb(4, 137, 183) !important; cursor: default; " title="en/Core JavaScript 1.5 Reference/Global
Objects/Object/keys">Object.keys</a>) over the properties of the object.  The ordering of the non-enumerable properties in the array, and among the enumerable properties, is not defined.</p>
<h3>Examples</h3>
<pre class="brush: js">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"
</pre>
<p>If you want only the enumerable properties, see <a href="/en/JavaScript/Reference/Global_Objects/Object/keys" title="en/Core JavaScript 1.5 Reference/Global
Objects/Object/keys">Object.keys</a> or use a <a href="/en/JavaScript/Reference/Statements/for...in" title="en/Core JavaScript 1.5 Reference/Statements/for...in">for...in loop</a> (although note that this will return enumerable properties not found directly upon that object but also along the prototype chain for the object).</p>
<h3 name="See_also">See also</h3>
<ul> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="en/Core JavaScript 1.5 Reference/Global
    Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/create" title="en/Core JavaScript 1.5 Reference/Global
    Objects/Object/create">Object.create</a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/keys" title="en/Core JavaScript 1.5 Reference/Global
    Objects/Object/keys">Object.keys</a></li> <li><a class=" link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=518663">Bug 518663</a> -<span id="summary_alias_container"> <span id="short_desc_nonedit_display">ES5: Object.getOwnPropertyNames</span> </span></li>
</ul>
<dl> <dt>
</dt></dl>
Revert to this revision