--- 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 ---
{{JSRef}}

Il metodo hasOwnProperty() restituisce un valore booleano che indica se l'oggetto ha la proprietà specificata come propria proprietà (invece di ereditarla).

{{EmbedInteractiveExample("pages/js/object-prototype-hasownproperty.html")}}

Sintassi

obj.hasOwnProperty(prop)

Parametri

prop
Il nome della {{jsxref("String")}} o il {{Glossary("Symbol")}} della proprietà da testare.

Valore di ritorno

Un {{jsxref("Boolean")}} che indica se l'oggetto ha o meno la proprietà specificata come proprietà propria.

Descrizione

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.

Note

hasOwnProperty restituisce true anche se il valore della proprietà è nullundefined.

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 {{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
  }
}

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
{{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.

Compatibilità con i browser

{{Compat("javascript.builtins.Object.hasOwnProperty")}}

Vedi anche