Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 731581 of Object.getOwnPropertyNames()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
  • Revision title: Object.getOwnPropertyNames()
  • Revision id: 731581
  • Created:
  • Creator: antonradev
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Object")}}

Summary

The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.

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 {{jsxref("Statements/for...in", "for...in")}} loop (or by {{jsxref("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

Example: Using getOwnPropertyNames

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'

// Printing property names and values using Array.forEach
Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
  print(val + ' -> ' + obj[val]);
});
// prints
// 0 -> a
// 1 -> b
// 2 -> c

// 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 {{jsxref("Object.keys()")}} or use a {{jsxref("Statements/for...in", "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 unless the latter is filtered with {{jsxref("Object.prototype.hasOwnProperty()", "hasOwnProperty()")}}).

Items on the prototype chain are not listed:

function ParentClass() {}
ParentClass.prototype.inheritedMethod = function() {};

function ChildClass() {
  this.prop = 5;
  this.method = function() {};
}
ChildClass.prototype = new ParentClass;
ChildClass.prototype.prototypeMethod = function() {};

alert(
  Object.getOwnPropertyNames(
    new ChildClass() // ['prop', 'method']
  )
);

Example: Get Non-Enumerable Only

This uses the {{jsxref("Array.prototype.filter()")}} function to remove the enumerable keys (obtained with {{jsxref("Object.keys()")}}) from a list of all keys (obtained with Object.getOwnPropertyNames) leaving only the non-enumerable keys.

var target = myObject;
var enum_and_nonenum = Object.getOwnPropertyNames(target);
var enum_only = Object.keys(target);
var nonenum_only = enum_and_nonenum.filter(function(key) {
  var indexInEnum = enum_only.indexOf(key);
  if (indexInEnum == -1) {
    // not found in enum_only keys mean the key is non-enumerable,
    // so return true so we keep this in the filter
    return true;
  } else {
    return false;
  }
});

console.log(nonenum_only);

Notes

In ES5, if the argument to this method is not an object (a primitive), then it will cause a {{jsxref("Global_Objects/TypeError", "TypeError")}}. In ES6, a non-object argument will be coerced to an object.

> Object.getOwnPropertyNames("foo")
TypeError: "foo" is not an object // ES5 code

> Object.getOwnPropertyNames("foo")
["length", "0", "1", "2"]         // ES6 code

Specifications

Specification Status Comment
{{SpecName('ES5.1', '#sec-15.2.3.4', 'Object.getOwnPropertyNames')}} {{Spec2('ES5.1')}} Initial definition. Implemented in JavaScript 1.8.5.
{{SpecName('ES6', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}} {{Spec2('ES6')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome("5")}} {{CompatGeckoDesktop("2")}} {{CompatIE("9")}} {{CompatOpera("12")}} {{CompatSafari("5")}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

Based on Kangax's compat table.

SpiderMonkey-specific notes

Prior to SpiderMonkey 28 {{geckoRelease("28")}}, Object.getOwnPropertyNames did not see unresolved properties of {{jsxref("Error")}} objects. This has been fixed in later versions ({{bug("724768")}}).

See also

  • Enumerability and ownership of properties
  • {{jsxref("Object.prototype.hasOwnProperty()")}}
  • {{jsxref("Object.prototype.propertyIsEnumerable()")}}
  • {{jsxref("Object.create()")}}
  • {{jsxref("Object.keys()")}}
  • {{jsxref("Array.forEach()")}}

Revision Source

<div>{{JSRef("Global_Objects", "Object")}}</div>

<h2 id="Summary" name="Summary">Summary</h2>

<p>The <code><strong>Object.getOwnPropertyNames()</strong></code> method returns an array of all properties (enumerable or not) found directly upon a given object.</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox">
<code>Object.getOwnPropertyNames(<var>obj</var>)</code></pre>

<h3 id="Parameters" name="Parameters">Parameters</h3>

<dl>
 <dt><code>obj</code></dt>
 <dd>The object whose enumerable <em>and non-enumerable</em> own properties are to be returned.</dd>
</dl>

<h2 id="Description" name="Description">Description</h2>

<p><code>Object.getOwnPropertyNames</code> 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 {{jsxref("Statements/for...in", "for...in")}} loop (or by {{jsxref("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.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Example.3A_Using_getOwnPropertyNames">Example: Using <code>getOwnPropertyNames</code></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'

// Printing property names and values using Array.forEach
Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
  print(val + ' -&gt; ' + obj[val]);
});
// prints
// 0 -&gt; a
// 1 -&gt; b
// 2 -&gt; c

// non-enumerable property
var my_obj = Object.create({}, { getFoo: { 
&nbsp; value: function() { return this.foo; },
&nbsp; 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 {{jsxref("Object.keys()")}} or use a {{jsxref("Statements/for...in", "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 unless the latter is filtered with {{jsxref("Object.prototype.hasOwnProperty()", "hasOwnProperty()")}}).</p>

<p>Items on the prototype chain are not listed:</p>

<pre class="brush: js">
function ParentClass() {}
ParentClass.prototype.inheritedMethod = function() {};

function ChildClass() {
  this.prop = 5;
  this.method = function() {};
}
ChildClass.prototype = new ParentClass;
ChildClass.prototype.prototypeMethod = function() {};

alert(
  Object.getOwnPropertyNames(
    new ChildClass() // ['prop', 'method']
  )
);
</pre>

<h3 id="Example.3A_Get_Non-Enumerable_Only">Example: Get Non-Enumerable Only</h3>

<p>This uses the {{jsxref("Array.prototype.filter()")}} function to remove the enumerable keys (obtained with {{jsxref("Object.keys()")}}) from a list of all keys (obtained with <code>Object.getOwnPropertyNames</code>) leaving only the non-enumerable keys.</p>

<pre class="brush: js">
var target = myObject;
var enum_and_nonenum = Object.getOwnPropertyNames(target);
var enum_only = Object.keys(target);
var nonenum_only = enum_and_nonenum.filter(function(key) {
  var indexInEnum = enum_only.indexOf(key);
  if (indexInEnum == -1) {
    // not found in enum_only keys mean the key is non-enumerable,
    // so return true so we keep this in the filter
    return true;
  } else {
    return false;
  }
});

console.log(nonenum_only);
</pre>

<h2 id="Notes" name="Notes">Notes</h2>

<p>In ES5, if the argument to this method is not an object (a primitive), then it will cause a {{jsxref("Global_Objects/TypeError", "TypeError")}}. In ES6, a non-object argument will be coerced to an object.</p>

<pre class="brush: js">
&gt; Object.getOwnPropertyNames("foo")
TypeError: "foo" is not an object // ES5 code

&gt; Object.getOwnPropertyNames("foo")
["length", "0", "1", "2"]         // ES6 code
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2.3.4', 'Object.getOwnPropertyNames')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.8.5.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome("5")}}</td>
   <td>{{CompatGeckoDesktop("2")}}</td>
   <td>{{CompatIE("9")}}</td>
   <td>{{CompatOpera("12")}}</td>
   <td>{{CompatSafari("5")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>Based on <a href="https://kangax.github.com/es5-compat-table/">Kangax's compat table</a>.</p>

<h3 id="SpiderMonkey-specific_notes">SpiderMonkey-specific notes</h3>

<p>Prior to SpiderMonkey 28 {{geckoRelease("28")}}, <code>Object.getOwnPropertyNames</code> did not see unresolved properties of {{jsxref("Error")}} objects. This has been fixed in later versions ({{bug("724768")}}).</p>

<h2 id="See_also" name="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
 <li>{{jsxref("Object.prototype.hasOwnProperty()")}}</li>
 <li>{{jsxref("Object.prototype.propertyIsEnumerable()")}}</li>
 <li>{{jsxref("Object.create()")}}</li>
 <li>{{jsxref("Object.keys()")}}</li>
 <li>{{jsxref("Array.forEach()")}}</li>
</ul>
Revert to this revision