aboutsummaryrefslogtreecommitdiff
path: root/files/ca/web/javascript/reference/global_objects/object/keys/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/ca/web/javascript/reference/global_objects/object/keys/index.html')
-rw-r--r--files/ca/web/javascript/reference/global_objects/object/keys/index.html189
1 files changed, 189 insertions, 0 deletions
diff --git a/files/ca/web/javascript/reference/global_objects/object/keys/index.html b/files/ca/web/javascript/reference/global_objects/object/keys/index.html
new file mode 100644
index 0000000000..17d05b181c
--- /dev/null
+++ b/files/ca/web/javascript/reference/global_objects/object/keys/index.html
@@ -0,0 +1,189 @@
+---
+title: Object.keys()
+slug: Web/JavaScript/Reference/Global_Objects/Object/keys
+translation_of: Web/JavaScript/Reference/Global_Objects/Object/keys
+---
+<div>{{JSRef}}</div>
+
+<p>El mètode <code><strong>Object.keys()</strong></code> retorna un array de les propietats enumerables pròpies de l'objecte, en el mateix ordre en que es donarien en un bucle {{jsxref("Statements/for...in", "for...in")}} (la diferència radica en que el bucle for-in també enumera les propietats que pertanyen a la cadena de prototipus).</p>
+
+<h2 id="Sintaxi">Sintaxi</h2>
+
+<pre class="syntaxbox"><code>Object.keys(<var>obj</var>)</code></pre>
+
+<h3 id="Paràmetres">Paràmetres</h3>
+
+<dl>
+ <dt><code>obj</code></dt>
+ <dd>L'objecte del qual es retornaran les seves propietats pròpies enumerables.</dd>
+</dl>
+
+<h2 id="Descripció">Descripció</h2>
+
+<p><code>Object.keys()</code> retorna un array els elements del qual són strings que corresponen a les propietats enumerables que pertanyen directament a l'objecte <code>obj</code>. L'ordre de les propietats és el mateix que el donat per recòrrer les propieats de l'objecte de forma manual.</p>
+
+<h2 id="Exemples">Exemples</h2>
+
+<pre class="brush: js">var arr = ['a', 'b', 'c'];
+console.log(Object.keys(arr)); // console: ['0', '1', '2']
+
+// objecte en forma d'array
+var obj = { 0: 'a', 1: 'b', 2: 'c' };
+console.log(Object.keys(obj)); // console: ['0', '1', '2']
+
+// objecte en forma d'array amb les claus ordenades de manera aleatòria
+var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
+console.log(Object.keys(an_obj)); // console: ['2', '7', '100']
+
+// getFoo és una propietat no enumerable
+var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; } } });
+my_obj.foo = 1;
+
+console.log(Object.keys(my_obj)); // console: ['foo']
+</pre>
+
+<p>Si esteu interessats en obtenir totes les propietats, no només les enumerables, vegeu {{jsxref("Object.getOwnPropertyNames()")}}.</p>
+
+<h2 id="Notes">Notes</h2>
+
+<p>A l'EcmaScript 5 llençarà una excepció <a href="https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Objectes_globals/TypeError" title="L'objecte TypeError representa un error quan el valor no és del tipus esperat."><code>TypeError</code></a> si el paràmetre <code>obj</code> no és un objecte. A l'EcmaScript 6 el paràmetre serà forçat al tipus <a class="new" href="https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Object" title="Aquesta pàgina encara no ha estat traduïda. Si us plau considera contribuir-hi!"><code>Object</code></a>.</p>
+
+<pre class="brush: js">Object.keys("foo");
+// TypeError: "foo" no és un objecte (codi EcmaScript 5)
+Object.keys("foo");
+// ["0", "1", "2"] (codi EcmaScript 6)
+</pre>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<p>Per a afegit una funció compatible amb <code>Object.keys</code> en plataformes que no la suporten de forma nativa, podeu utilitzar el codi següent:</p>
+
+<pre class="brush: js">// Tret de https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
+if (!Object.keys) {
+ Object.keys = (function() {
+ 'use strict';
+ var hasOwnProperty = Object.prototype.hasOwnProperty,
+ hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
+ dontEnums = [
+ 'toString',
+ 'toLocaleString',
+ 'valueOf',
+ 'hasOwnProperty',
+ 'isPrototypeOf',
+ 'propertyIsEnumerable',
+ 'constructor'
+ ],
+ dontEnumsLength = dontEnums.length;
+
+ return function(obj) {
+ if (typeof obj !== 'object' &amp;&amp; (typeof obj !== 'function' || obj === null)) {
+ throw new TypeError('Object.keys called on non-object');
+ }
+
+ var result = [], prop, i;
+
+ for (prop in obj) {
+ if (hasOwnProperty.call(obj, prop)) {
+ result.push(prop);
+ }
+ }
+
+ if (hasDontEnumBug) {
+ for (i = 0; i &lt; dontEnumsLength; i++) {
+ if (hasOwnProperty.call(obj, dontEnums[i])) {
+ result.push(dontEnums[i]);
+ }
+ }
+ }
+ return result;
+ };
+ }());
+}
+</pre>
+
+<p>Cal destacar que aquest codi inclou també propietats no enumerables a Internet Explorer 7 (i possiblement també a Internet Explorer 8), al passar un objecte pertanyent a una altra finestra.</p>
+
+<p>Per a una versió simplificada Polyfill per a navegadors vegeu <a href="http://tokenposts.blogspot.com.au/2012/04/javascript-objectkeys-browser.html">Compatibilitat de navegadors amb la funció Object.keys de JavaScript</a>.</p>
+
+<h2 id="Especificacions">Especificacions</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Especificació</th>
+ <th scope="col">Estat</th>
+ <th scope="col">Comentaris</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.2.3.14', 'Object.keys')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td>Definició inicial. Implementat a 1.8.5.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-object.keys', 'Object.keys')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Característica</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Suport bàsic</td>
+ <td>{{CompatChrome("5")}}</td>
+ <td>{{CompatGeckoDesktop("2.0")}}</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>Característica</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>Suport bàsic</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="Vegeu_també">Vegeu també</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerabilitat i possessió de propietats</a></li>
+ <li>{{jsxref("Object.prototype.propertyIsEnumerable()")}}</li>
+ <li>{{jsxref("Object.create()")}}</li>
+ <li>{{jsxref("Object.getOwnPropertyNames()")}}</li>
+</ul>