diff options
Diffstat (limited to 'files/it/web/javascript/reference/global_objects/object/hasownproperty/index.html')
| -rw-r--r-- | files/it/web/javascript/reference/global_objects/object/hasownproperty/index.html | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/global_objects/object/hasownproperty/index.html b/files/it/web/javascript/reference/global_objects/object/hasownproperty/index.html new file mode 100644 index 0000000000..7287ed1e18 --- /dev/null +++ b/files/it/web/javascript/reference/global_objects/object/hasownproperty/index.html @@ -0,0 +1,164 @@ +--- +title: Object.prototype.hasOwnProperty() +slug: Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty +tags: + - JavaScript + - Object + - Prototype + - hasOwnProperty + - metodo +translation_of: Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty +--- +<div>{{JSRef}}</div> + +<p>Il metodo <strong><code>hasOwnProperty()</code></strong> restituisce un valore booleano che indica se l'oggetto ha la proprietà specificata come propria proprietà (invece di ereditarla).</p> + +<div>{{EmbedInteractiveExample("pages/js/object-prototype-hasownproperty.html")}}</div> + + + +<h2 id="Sintassi">Sintassi</h2> + +<pre class="syntaxbox"><var>obj</var>.hasOwnProperty(<var>prop</var>)</pre> + +<h3 id="Parametri">Parametri</h3> + +<dl> + <dt><var>prop</var></dt> + <dd>Il nome della {{jsxref("String")}} o il {{Glossary("Symbol")}} della proprietà da testare.</dd> +</dl> + +<h3 id="Valore_di_ritorno">Valore di ritorno</h3> + +<p>Un {{jsxref("Boolean")}} che indica se l'oggetto ha o meno la proprietà specificata come proprietà propria.</p> + +<h2 id="Descrizione">Descrizione</h2> + +<p>Tutti i discendenti di {{jsxref("Object")}} ereditano il metodo <code>hasOwnProperty</code>. Questo metodo può essere utilizzato per determinare se un oggetto ha la proprietà specificata come proprietà diretta di tale oggetto; a differenza dell'operatore {{jsxref("Operators/in", "in")}}, questo metodo non controlla una proprietà nella catena di prototipi dell'oggetto.</p> + +<h2 id="Note">Note</h2> + +<p><code>hasOwnProperty</code> restituisce true anche se il valore della proprietà è <code>null</code> o <code>undefined</code>.</p> + +<pre class="brush: js">o = new Object(); +o.propOne = null; +o.hasOwnProperty('propOne'); // ritorna true +o.propTwo = undefined; +o.hasOwnProperty('propTwo'); // ritorna true +</pre> + +<h2 id="Esempi">Esempi</h2> + +<h3 id="Usare_hasOwnProperty_per_verificare_l'esistenza_di_una_proprietà">Usare <code>hasOwnProperty</code> per verificare l'esistenza di una proprietà</h3> + +<p>L'esempio seguente determina se l'oggetto o contiene una proprietà denominata <code>prop</code>:</p> + +<pre class="brush: js">o = new Object(); +o.hasOwnProperty('prop'); // ritorna false +o.prop = 'exists'; +o.hasOwnProperty('prop'); // ritorna true +</pre> + +<h3 id="Dirette_vs._proprietà_ereditate">Dirette vs. proprietà ereditate</h3> + +<p>Il seguente esempio distingue tra proprietà dirette e proprietà ereditate attraverso la catena del prototipo:</p> + +<pre class="brush: js">o = new Object(); +o.prop = 'exists'; +o.hasOwnProperty('prop'); // ritorna true +o.hasOwnProperty('toString'); // ritorna false +o.hasOwnProperty('hasOwnProperty'); // ritorna false +</pre> + +<h3 id="Iterare_sulle_proprietà_di_un_oggetto">Iterare sulle proprietà di un oggetto</h3> + +<p>L'esempio seguente mostra come eseguire iterazioni sulle proprietà di un oggetto senza eseguire l'esecuzione su proprietà ereditate. Si noti che il ciclo {{jsxref("Statements/for...in", "for...in")}} sta già solo iterando gli oggetti enumerabili, quindi non si dovrebbe assumere in base alla mancanza di proprietà non enumerabili mostrate nel ciclo che <code>hasOwnProperty</code> è strettamente limitato agli elementi enumerabili (come con {{jsxref("Object.getOwnPropertyNames()")}}).</p> + +<pre class="brush: js">var buz = { + fog: 'stack' +}; + +for (var name in buz) { + if (buz.hasOwnProperty(name)) { + console.log('this is fog (' + + name + ') for sure. Value: ' + buz[name]); + } + else { + console.log(name); // toString o qualcos'altro + } +} +</pre> + +<h3 id="Usare_hasOwnProperty_come_nome_di_una_proprietà">Usare <code>hasOwnProperty</code> come nome di una proprietà</h3> + +<p>JavaScript non protegge il nome della proprietà <code>hasOwnProperty</code>; quindi, se esiste la possibilità che un oggetto possa avere una proprietà con questo nome, è necessario utilizzare un <code>hasOwnProperty</code> <em>esterno</em> per ottenere risultati corretti:</p> + +<pre class="brush: js">var foo = { + hasOwnProperty: function() { + return false; + }, + bar: 'Here be dragons' +}; + +foo.hasOwnProperty('bar'); // restituisce sempre false + +// Usare hasOwnProperty di un altro oggetto +// e chiamarlo con 'this' impostato su foo +({}).hasOwnProperty.call(foo, 'bar'); // true + +// È anche possibile utilizzare la proprietà hasOwnProperty +// dal prototipo Object per questo scopo +Object.prototype.hasOwnProperty.call(foo, 'bar'); // true +</pre> + +<p>Nota che nell'ultimo caso non ci sono oggetti appena creati.</p> + +<h2 id="Specifiche">Specifiche</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specifica</th> + <th scope="col">Stato</th> + <th scope="col">Commento</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-object.prototype.hasownproperty', 'Object.prototype.hasOwnProperty')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.prototype.hasownproperty', 'Object.prototype.hasOwnProperty')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.4.5', 'Object.prototype.hasOwnProperty')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Definizione iniziale Implementato in JavaScript 1.5.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilità_con_i_browser">Compatibilità con i browser</h2> + + + +<p>{{Compat("javascript.builtins.Object.hasOwnProperty")}}</p> + +<h2 id="Vedi_anche">Vedi anche</h2> + +<ul> + <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerabilità e proprietà delle proprietà</a></li> + <li>{{jsxref("Object.getOwnPropertyNames()")}}</li> + <li>{{jsxref("Statements/for...in", "for...in")}}</li> + <li>{{jsxref("Operators/in", "in")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain">Guida JavaScript: Ereditarietà rivisitata</a></li> +</ul> |
