aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/javascript/reference/functions/get/index.html
blob: 64ecd47f912df663d41a07874d57c54d8ab3a97d (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
---
title: get
slug: Web/JavaScript/Reference/Functions/get
tags:
  - ECMAScript5
  - JavaScript
  - Operator
translation_of: Web/JavaScript/Reference/Functions/get
original_slug: Web/JavaScript/Referencia/Funciones/get
---
<div>{{jsSidebar("Funciones")}}</div>

<p>Enlaza la propiedad de un objeto con una función que será llamada cuando la propiedad es buscada.</p>

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

<p><code>{get <em>prop</em>() { . . . } }</code></p>

<h2 id="Parámetros">Parámetros</h2>

<dl>
 <dt><code>prop</code></dt>
 <dd>el nombre de la propiedad a unir con la función dada</dd>
</dl>

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

<p>A veces es deseable permitir acceso a una propiedad que retorna un valor dinámicamente calculado, o si desea mostrar el estado de alguna variable interna sin requerir el uso de llamadas a métodos explícitos. En JavaScript, esto se puede lograr con el uso de un getter (captador). No es posible tener simultáneamente un getter ligado a una propiedad y que dicha propiedad tenga actualmente un valor, aunque es posible usar un getter junto con un setter para crear un tipo de pseudo-propiedad.</p>

<p>Tenga en cuenta lo siguiente al trabajar con la sintaxis <code>get</code>:</p>

<ul>
 <li>Puede tener un identificador que sea un número o una cadena.</li>
 <li>Debe tener exactamente cero parametros (ver <a href="http://whereswalden.com/2010/08/22/incompatible-es5-change-literal-getter-and-setter-functions-must-now-have-exactly-zero-or-one-arguments/">Cambio ES5 incompatible: las funciones getter y setter literal deben tener ahora exactamente cero o un argumento </a>para mas información);</li>
 <li>No debe haber múltiples getters para una misma propiedad (<code>{ get x() { }, get x() { } }</code> y <code>{ x: ..., get x() { } }</code> están prohibidos).</li>
</ul>

<p>El getter puede ser removido usando el operador {{jsxref("Operadores/delete", "delete")}}.</p>

<h2 id="Ejemplos">Ejemplos</h2>

<h3 id="Example:_Defining_a_getter_with_the_get_operator" name="Example:_Defining_a_getter_with_the_get_operator">Definir un getter con el operador <code>get</code></h3>

<p>Esto creará una pseudo-propiedad <code>latest</code> (ver ejemplo) del objecto <code>o</code> que podría retornar la más reciente entrada dentro de <code>o.log</code>:</p>

<pre class="brush: js">var o = {
  get latest () {
    if (this.log.length &gt; 0) {
      return this.log[this.log.length - 1];
    }
    else {
      return null;
    }
  },
  log: []
}
</pre>

<p>Note que intentar asignar un valor a <code>latest</code> no lo cambiará.</p>

<h3 id="Example:_Deleting_a_getter_using_the_delete_operator" name="Example:_Deleting_a_getter_using_the_delete_operator">Borrar un getter usando el operador <code>delete</code></h3>

<pre class="brush: js">delete o.latest;
</pre>

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

<p>Basado en la página de <a class="external" href="http://robertnyman.com/javascript/javascript-getters-setters.html#regular-getters-and-setters">página de Robert Nyman</a></p>

<p>Sin soporte (notablemente en IE6-8) significa que el script lanzará un error de sintaxis.</p>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</th>
   <th>Firefox (Gecko)</th>
   <th>Chrome</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Soporte básico</td>
   <td>{{ CompatGeckoDesktop("1.8.1") }}</td>
   <td>1</td>
   <td>9</td>
   <td>9.5</td>
   <td>3</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Android</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Soporte básico</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also" name="See_also">Consulta también</h2>

<ul>
 <li>{{jsxref("Operators/delete", "delete")}}</li>
 <li>{{jsxref("Operators/set", "set")}}</li>
 <li>{{jsxref("Object.defineProperty()")}}</li>
 <li>{{jsxref("Object.defineGetter", "__defineGetter__")}}</li>
 <li>{{jsxref("Object.defineSetter", "__defineSetter__")}}</li>
 <li><a href="/es/JavaScript/Guide/Working_with_Objects#Defining_Getters_and_Setters" title="en/JavaScript/Guide/Working with Objects#Defining Getters and Setters">Defining Getters and Setters</a> in JavaScript Guide</li>
</ul>

<div class="noinclude">
<p> </p>
</div>

<p> </p>