aboutsummaryrefslogtreecommitdiff
path: root/files/vi/web/javascript/reference/global_objects/object/getownpropertynames/index.html
blob: d1e533fceba40a751267e084398df535f7beef9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: Object.getOwnPropertyNames()
slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
translation_of: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
---
<div>{{JSRef}}</div>

<p>The <strong><code>Object.getOwnPropertyNames()</code></strong> phuong thuc nay tra ve mang (including non-enumerable properties except for those which use Symbol) found directly in a given object.</p>

<div>{{EmbedInteractiveExample("pages/js/object-getownpropertynames.html")}}</div>

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

<pre class="brush: js">Object.getOwnPropertyNames(<var>obj</var>)</pre>

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

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

<h3 id="Return_value">Return value</h3>

<p>An array of strings that corresponds to the properties found directly in the given object.</p>

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

<p><code>Object.getOwnPropertyNames()</code> returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly in a given object <code><var>obj</var></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 the ordering among the enumerable properties is not defined.</p>

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

<h3 id="Using_Object.getOwnPropertyNames">Using <code>Object.getOwnPropertyNames()</code></h3>

<pre class="brush: js">var arr = ['a', 'b', 'c'];
console.log(Object.getOwnPropertyNames(arr).sort());
// logs ["0", "1", "2", "length"]

// Array-like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.getOwnPropertyNames(obj).sort());
// logs ["0", "1", "2"]

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

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

console.log(Object.getOwnPropertyNames(my_obj).sort());
// logs ["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 (note that this will also return enumerable properties found 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() {};

console.log(
  Object.getOwnPropertyNames(
    new ChildClass() // ["prop", "method"]
  )
);
</pre>

<h3 id="Get_non-enumerable_properties_only">Get non-enumerable properties 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>) thus giving only the non-enumerable keys as output.</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,
    // meaning that 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">Notes</h2>

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

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

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

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}}</td>
  </tr>
 </tbody>
</table>

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



<p>{{Compat("javascript.builtins.Object.getOwnPropertyNames")}}</p>

<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>

<p>Prior to Firefox 28, <code>Object.getOwnPropertyNames</code> did not see unresolved properties of {{jsxref("Error")}} objects. This has been fixed in later versions (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=724768">bug 724768</a>).</p>

<h2 id="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>