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
|
---
title: Object.getOwnPropertyNames()
slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
translation_of: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
original_slug: Web/JavaScript/Referencia/Objetos_globales/Object/getOwnPropertyNames
---
<div>
{{JSRef("Global_Objects", "Object")}}</div>
<h2 id="Summary" name="Summary">Resumen</h2>
<p>El método <code><strong>Object.getOwnPropertyNames()</strong></code> devuelve un array con todas las propiedades (numerables o no) encontradas en un objeto dado.</p>
<h2 id="Syntax" name="Syntax">Sintaxis</h2>
<pre class="syntaxbox"><code>Object.getOwnPropertyNames(<em>obj</em>)</code></pre>
<h3 id="Parameters" name="Parameters">Parámetros</h3>
<dl>
<dt>
obj</dt>
<dd>
El objeto cuyas propiedades directas, numerables <em>y no-numerables</em>, serán devueltas.</dd>
</dl>
<h2 id="Description" name="Description">Descripción</h2>
<p><code>Object.getOwnPropertyNames</code> devuelve un array cuyos elementos son <em>strings </em>correspondientes a cada una de las propiedades encontradas directamente en <code>obj</code>. El orden de las propiedades numerables en el array coincide con el expuesto para <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in loop</a> (o para {{jsxref("Object.keys")}}) con respecto a las propiedades del object. El orden de las propiedades no-numerables del array, y de éstas respecto a las numerables, no está definido.</p>
<h2 id="Ejemplos">Ejemplos</h2>
<pre class="brush: js">var arr = ["a", "b", "c"];
print(Object.getOwnPropertyNames(arr).sort()); // imprime "0,1,2,length"
// Objeto similar a Array
var obj = { 0: "a", 1: "b", 2: "c"};
print(Object.getOwnPropertyNames(obj).sort()); // imprime "0,1,2"
// Imprime nombres de variables y valores usando Array.forEach
Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
print(val + " -> " + obj[val]);
});
// imprime
// 0 -> a
// 1 -> b
// 2 -> c
// propiedad no-numerable
var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; }, enumerable: false } });
my_obj.foo = 1;
print(Object.getOwnPropertyNames(my_obj).sort()); // imprime "foo, getFoo"
</pre>
<p>Si se quiere solo las propiedades numerables, ver {{jsxref("Object.keys")}} o usar un <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in loop</a> (aunque esto devolvería propiedades numerables no directas del <span style="line-height: 1.5;">objeto pertenecientes a la cadena de <em>prototype</em> a la que pertenezca, a menos que finalmente se filtre con hasOwnProperty()).</span></p>
<p>Items de la cadena <em>prototype</em> no se listan:</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="Get_Non-Enumerable_Only">Get Non-Enumerable Only</h3>
<p>Aquí se usa la función Array.prototype.filter para quitar las <em>keys</em> numerables (obtenidas con Object.keys) de una lista con todas las <em>keys</em> (obtenida con Object.getOwnPropertynames) dejando solo las no-numerables.</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) {
//no encontrada en las keys de enum_only, por lo que se trata de una key numerable, se devuelve true para mantenerla en filter
return true;
} else {
return false;
}
});
console.log(nonenum_only);
</pre>
<h2 id="Especificaciones">Especificaciones</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificación</th>
<th scope="col">Status</th>
<th scope="col">Comentario</th>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.2.3.4', 'Object.getOwnPropertyNames')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Initial definition.<br>
Implemented in JavaScript 1.8.5</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Compatibilidad_con_Navegadores"> Compatibilidad con Navegadores</h2>
<div>
{{Compat("javascript.builtins.Object.getOwnPropertyNames")}}
<h2 id="See_also" name="See_also">Ver también</h2>
<ul>
<li><a href="/en-US/docs/Enumerability_and_ownership_of_properties" title="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>
|