From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- .../global_objects/object/keys/index.html | 167 +++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 files/it/web/javascript/reference/global_objects/object/keys/index.html (limited to 'files/it/web/javascript/reference/global_objects/object/keys') diff --git a/files/it/web/javascript/reference/global_objects/object/keys/index.html b/files/it/web/javascript/reference/global_objects/object/keys/index.html new file mode 100644 index 0000000000..ed748c0fad --- /dev/null +++ b/files/it/web/javascript/reference/global_objects/object/keys/index.html @@ -0,0 +1,167 @@ +--- +title: Object.keys() +slug: Web/JavaScript/Reference/Global_Objects/Object/keys +tags: + - ECMAScript5 + - JavaScript + - JavaScript 1.8.5 + - Metodi + - Oggetti +translation_of: Web/JavaScript/Reference/Global_Objects/Object/keys +--- +
+ {{JSRef("Global_Objects", "Object")}}
+

Sommario

+

Il metodo Object.keys() restituisce un array contenente le proprietà enumerabili di un dato oggetto, nel medesimo ordine fornito da un ciclo for...in  (la differenza è che un ciclo for-in enumera anche le proprietà nella catena di prototipi).

+

Sintassi

+
Object.keys(obj)
+

Parametri

+
+
+ obj
+
+ L'oggetto del quale si devono restituire le proprietà enumerabili.
+
+

Descrizione

+

Object.keys restituisce un array i quali elementi sono stringhe corrispondenti alle proprietà enumerabili trovate direttamente in obj. L'ordine delle proprietà è lo stesso di quello dato ciclando manualmente sulle proprietà dell'oggetto.

+

Esempi

+
var arr = ["a", "b", "c"];
+alert(Object.keys(arr)); // chiama alert con argomento "0,1,2"
+
+// array like object
+var obj = { 0 : "a", 1 : "b", 2 : "c"};
+alert(Object.keys(obj)); // chiama alert con argomento "0,1,2"
+
+// array like object with random key ordering
+var an_obj = { 100: "a", 2: "b", 7: "c"};
+alert(Object.keys(an_obj)); // chiama alert con argomento "2, 7, 100"
+
+// getFoo is property which isn't enumerable
+var my_obj = Object.create({}, { getFoo : { value : function () { return this.foo } } });
+my_obj.foo = 1;
+
+alert(Object.keys(my_obj)); // chiama alert con foo come unico argomento
+
+

Per ottenere tutte le proprietà, anche quelle non enumerabili, si veda {{jsxref("Object.getOwnPropertyNames")}}.

+

Polyfill

+

Per aggiungere un supporto equivalente a Object.keys, in ambienti datati che non lo supportino nativamente, si copi il seguente frammento di codice:

+
// Da 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' && (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 < dontEnumsLength; i++) {
+          if (hasOwnProperty.call(obj, dontEnums[i])) {
+            result.push(dontEnums[i]);
+          }
+        }
+      }
+      return result;
+    };
+  }());
+}
+
+

Si noti che il codice sopra include chiavi non-enumerabili in IE7 (e forse IE8), nel caso in cui si passi un oggetto proveniente da un'altra finestra.

+

Per un semplice polyfill, si veda Javascript - Object.keys Browser Compatibility.

+

Specifiche

+ + + + + + + + + + + + + + + + + + +
SpecificaStatoCommento
{{SpecName('ES5.1', '#sec-15.2.3.14', 'Object.keys')}}{{Spec2('ES5.1')}}Definizione iniziale.
+ implementato in in JavaScript 1.8.5
{{SpecName('ES6', '#sec-object.keys', 'Object.keys')}}{{Spec2('ES6')}} 
+

Compatibilità dei browser

+
+ {{CompatibilityTable}}
+
+ + + + + + + + + + + + + + + + + + + +
FeatureFirefox (Gecko)ChromeInternet ExplorerOperaSafari
Supporto base4 (2.0)59125
+
+
+ + + + + + + + + + + + + + + + + + + +
FeatureFirefox Mobile (Gecko)AndroidIE MobileOpera MobileSafari Mobile
Supporto base{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+

Basato su Kangax's compat table.

+

Vedere anche

+ -- cgit v1.2.3-54-g00ecf