--- 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 ---
Il metodo hasOwnProperty() restituisce un valore booleano che indica se l'oggetto ha la proprietà specificata come propria proprietà (invece di ereditarla).
obj.hasOwnProperty(prop)
Un {{jsxref("Boolean")}} che indica se l'oggetto ha o meno la proprietà specificata come proprietà propria.
Tutti i discendenti di {{jsxref("Object")}} ereditano il metodo hasOwnProperty. 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.
hasOwnProperty restituisce true anche se il valore della proprietà è null o undefined.
o = new Object();
o.propOne = null;
o.hasOwnProperty('propOne'); // ritorna true
o.propTwo = undefined;
o.hasOwnProperty('propTwo'); // ritorna true
hasOwnProperty per verificare l'esistenza di una proprietàL'esempio seguente determina se l'oggetto o contiene una proprietà denominata prop:
o = new Object();
o.hasOwnProperty('prop'); // ritorna false
o.prop = 'exists';
o.hasOwnProperty('prop'); // ritorna true
Il seguente esempio distingue tra proprietà dirette e proprietà ereditate attraverso la catena del prototipo:
o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop'); // ritorna true
o.hasOwnProperty('toString'); // ritorna false
o.hasOwnProperty('hasOwnProperty'); // ritorna false
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 hasOwnProperty è strettamente limitato agli elementi enumerabili (come con {{jsxref("Object.getOwnPropertyNames()")}}).
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
}
}
hasOwnProperty come nome di una proprietàJavaScript non protegge il nome della proprietà hasOwnProperty; quindi, se esiste la possibilità che un oggetto possa avere una proprietà con questo nome, è necessario utilizzare un hasOwnProperty esterno per ottenere risultati corretti:
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
Nota che nell'ultimo caso non ci sono oggetti appena creati.
| Specifica | Stato | Commento |
|---|---|---|
| {{SpecName('ESDraft', '#sec-object.prototype.hasownproperty', 'Object.prototype.hasOwnProperty')}} | {{Spec2('ESDraft')}} | |
| {{SpecName('ES6', '#sec-object.prototype.hasownproperty', 'Object.prototype.hasOwnProperty')}} | {{Spec2('ES6')}} | |
| {{SpecName('ES5.1', '#sec-15.2.4.5', 'Object.prototype.hasOwnProperty')}} | {{Spec2('ES5.1')}} | |
| {{SpecName('ES3')}} | {{Spec2('ES3')}} | Definizione iniziale Implementato in JavaScript 1.5. |
{{Compat("javascript.builtins.Object.hasOwnProperty")}}