★ wanayoo — archive 1999 https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnPropertyNouvelle recherche | Portail wanayoo

Object.prototype.hasOwnProperty()

von 1 Mitwirkenden:

Diese Übersetzung ist unvollständig. Bitte helfen Sie, diesen Artikel aus dem Englischen zu übersetzen.

Die Methode hasOwnProperty() gibt einen boolean Wert zurück abhängig von der Existenz des gegebenen Attributs in einem Objekt.

Syntax

obj.hasOwnProperty(prop)

Parameter

prop
Der Name des Attributs auf dessen Existenz im Objekt geprüft wird

Beschreibung

Jedes  untergeordnete Objekt / jeder untergeordneter Wert in einem Objekt trägt die hasOwnProperty Methode mit sich. Diese Methode ermöglicht das Nachfragen eines untergeordneten Wertes/Objekts innerhalb eines Objekts. Anders als die in Methode ermöglicht die hasOwnProperty Methode keinen Zugriff auf die Kindeskinder eines Objekts.

Beispiele

Einsatz von hasOwnProperty zur Überprüfung der Existenz eines Attributs in einem Objekt

Das folgende Beispiel prüft ob das Objekt o ein Attribut mit dem name prop beinhaltet.

o = new Object();
o.prop = 'exists';

function changeO() {
  o.newprop = o.prop;
  delete o.prop;
}

o.hasOwnProperty('prop');   // gibt true zurück
changeO();
o.hasOwnProperty('prop');   // gibt false zurück

Direkter Nachfolger vs Geerbtes Attribut

Das folgende Beispiel unterscheidet zwischen direkten Kind-Attributen eines Ojekts und den Aittributen die durch die prototype - Verarbeitung entstehen.

o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop');             // gibt true zurück
o.hasOwnProperty('toString');         // gibt false zurück
o.hasOwnProperty('hasOwnProperty');   // gibt false zurück

Iterating over the properties of an object

The following example shows how to iterate over the properties of an object without executing on inherit properties. Note that the for...in loop is already only iterating enumerable items, so one should not assume based on the lack of non-enumerable properties shown in the loop that hasOwnProperty itself is confined strictly to enumerable items (as with Object.getOwnPropertyNames()). - deutsche übersezung in Arbeit

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 or something else
  }
}

Using hasOwnProperty as a property name

JavaScript does not protect the property name hasOwnProperty; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an external hasOwnProperty to get correct results:

var foo = {
  hasOwnProperty: function() {
    return false;
  },
  bar: 'Here be dragons'
};

foo.hasOwnProperty('bar'); // always returns false

// Use another Object's hasOwnProperty and call it with 'this' set to foo
({}).hasOwnProperty.call(foo, 'bar'); // true

// It's also possible to use the hasOwnProperty property from the Object prototype for this purpose
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true

Note that in the last case there are no newly created objects.

Specifications

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.5.
ECMAScript 5.1 (ECMA-262)
Die Definition von 'Object.prototype.hasOwnProperty' in dieser Spezifikation.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
Die Definition von 'Object.prototype.hasOwnProperty' in dieser Spezifikation.
Standard  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Ja) (Ja) (Ja) (Ja) (Ja)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Ja) (Ja) (Ja) (Ja) (Ja) (Ja)

See also

Schlagwörter des Dokuments und Mitwirkende

Mitwirkende an dieser Seite: juicyarts
Zuletzt aktualisiert von: juicyarts,
Seitenleiste ausblenden