Il metodo hasOwnProperty() restituisce un valore booleano che indica se l'oggetto ha la proprietà specificata come propria proprietà (invece di ereditarla).
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Sintassi
obj.hasOwnProperty(prop)
Parametri
Valore di ritorno
Un Boolean che indica se l'oggetto ha o meno la proprietà specificata come proprietà propria.
Descrizione
Tutti i discendenti di 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 in, questo metodo non controlla una proprietà nella catena di prototipi dell'oggetto.
Note
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
Esempi
Usare 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
Dirette vs. proprietà ereditate
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
Iterare sulle proprietà di un oggetto
L'esempio seguente mostra come eseguire iterazioni sulle proprietà di un oggetto senza eseguire l'esecuzione su proprietà ereditate. Si noti che il ciclo 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 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
}
}
Usare 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.
Specifiche
| Specifica | Stato | Commento |
|---|---|---|
| ECMAScript Latest Draft (ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. |
Draft | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. |
Standard | |
| ECMAScript 5.1 (ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. |
Standard | |
| ECMAScript 3rd Edition (ECMA-262) | Standard | Definizione iniziale Implementato in JavaScript 1.5. |
Compatibilità con i browser
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
hasOwnProperty | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Legend
- Full support
- Full support