aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/javascript/reference/global_objects/object/propertyisenumerable/index.html
blob: 681dca1c2fab975d2338a2b09af947f7237c9521 (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
---
title: Object.prototype.propertyIsEnumerable()
slug: Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
tags:
  - JavaScript
  - Objecto
  - Property
  - metodo
translation_of: Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
original_slug: Web/JavaScript/Referencia/Objetos_globales/Object/propertyIsEnumerable
---
<div>{{JSRef}}</div>

<p>El método <code><strong>propertyIsEnumerable()</strong></code> regresa un Boleano indicando si la propiedad especificada es enumerable.</p>

<h2 id="Sintaxis">Sintaxis</h2>

<pre class="syntaxbox"><code><var>obj</var>.propertyIsEnumerable(<var>prop</var>)</code></pre>

<h3 id="Parametros">Parametros</h3>

<dl>
 <dt><code>prop</code></dt>
 <dd>Nombre de la propiedad a probar.</dd>
</dl>

<h2 id="Descripción">Descripción</h2>

<p>Todos los objetos tienen un método <code>propertyIsEnumerable</code>. Este método puede determinar si la propiedad especificada en el objeto puede ser enumerada por un ciclo {{jsxref("Statements/for...in", "for...in")}}, con la excepción de propiedades heredadas a través de prototype. Si el objeto no tiene la propiedad especificada, este método regresa un valor <code>false</code>.</p>

<h2 id="Ejemplo">Ejemplo</h2>

<h3 id="Uso_básico_de_propertyIsEnumerable">Uso básico de <code>propertyIsEnumerable</code></h3>

<p>El siguiente ejemplo muestra el uso de <code>propertyIsEnumerable</code> en objetos y arrays:</p>

<pre class="brush: js">var o = {};
var a = [];
o.prop = 'es enumerable';
a[0] = 'es enumerable';

o.propertyIsEnumerable('prop');   // regresa true
a.propertyIsEnumerable(0);        // regresa true
</pre>

<h3 id="Definidas_por_usuario_vs_predefinidas">Definidas por usuario vs predefinidas</h3>

<p>El siguiente ejemplo demuestra la enumerabilidad de las propiedades definidas por el usuario contra las predefinidas:</p>

<pre class="brush: js">var a = ['es enumerable'];

a.propertyIsEnumerable(0);          // regresa true
a.propertyIsEnumerable('length');   // regresa false

Math.propertyIsEnumerable('random');   // regresa false
this.propertyIsEnumerable('Math');     // regresa false
</pre>

<h3 id="Directa_vs_heredadas">Directa vs  heredadas</h3>

<pre class="brush: js">var a = [];
a.propertyIsEnumerable('constructor');         // regresa false

function primerConstructor() {
  this.propiedad = 'no es enumerable';
}

primerConstructor.prototype.primerMetodo = function() {};

function segundoConstructor() {
  this.metodo = function() { return 'es enumerable'; };
}

secondConstructor.prototype = new primerConstructor;
secondConstructor.prototype.constructor = segundoConstructor;

var o = new segundoConstructor();
o.propiedadArbitraria = 'is enumerable';

o.propertyIsEnumerable('propiedadArbitraria ');   // regresa true
o.propertyIsEnumerable('metodo');                 // regresa true
o.propertyIsEnumerable('propiedad');              // regresa false

o.propiedad = 'es enumerable';

o.propertyIsEnumerable('propiedad');              // regresa true

// Regresan false por estar en el prototipo el cual no es
// considerado por propertyIsEnumerable (a pesar de que las dos ultimas son
// iterables con un for-in)
o.propertyIsEnumerable('prototype');   // regresa false (como en JS 1.8.1/FF3.6)
o.propertyIsEnumerable('constructor'); // regresa false
o.propertyIsEnumerable('firstMethod'); // regresa false
</pre>

<h2 id="Especificaciones">Especificaciones</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Especificación</th>
   <th scope="col">Estatus</th>
   <th scope="col">Comentario</th>
  </tr>
  <tr>
   <td>{{SpecName('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Definición inicial.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2.4.7', 'Object.prototype.propertyIsEnumerable')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.prototype.propertyisenumerable', 'Object.prototype.propertyIsEnumerable')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilidad_de_navegadores">Compatibilidad de navegadores</h2>

{{Compat("javascript.builtins.Object.propertyIsEnumerable")}}

<h2 id="Notas_específicas_para_Gecko">Notas específicas para Gecko</h2>

<p>Al inicio de JavaScript 1.8.1 (in Firefox 3.6), <code>propertyIsEnumerable('prototype')</code> regresa <code>false</code> en lugar de <code>true</code>; esto hace que el resultado cumpla con la especificación de ECMAScript 5.</p>

<h2 id="Véase_también">Véase también</h2>

<ul>
 <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
 <li>{{jsxref("Statements/for...in", "for...in")}}</li>
 <li>{{jsxref("Object.keys()")}}</li>
 <li>{{jsxref("Object.defineProperty()")}}</li>
</ul>