La méthode hasOwnProperty () de l'objet JavaScript vérifie si l'objet a la propriété donnée comme sa propre propriété.
La syntaxe de la hasOwnProperty()méthode est:
obj.hasOwnProperty(prop)
Voici objun objet.
Paramètres hasOwnProperty ()
La hasOwnProperty()méthode prend en compte:
- prop - Le
Stringnom ou le symbole de la propriété à tester.
Valeur renvoyée par hasOwnProperty ()
- Renvoie un
Booleanindiquant si l'objet a ou non la propriété spécifiée comme sa propre propriété.
Remarques:
- Contrairement à l'
inopérateur, cette méthode ne vérifie pas une propriété dans la chaîne de prototypes de l'objet. hasOwnPropertyrenvoietruemême si la valeur de la propriété estnullouundefined.
Exemple: utilisation de hasOwnProperty ()
const obj = (); obj.property1 = 42; console.log(obj.hasOwnProperty("property1")); // true console.log(obj.hasOwnProperty("property2")); // false // Inherited properties return false console.log(obj.hasOwnProperty("toString")); // false
Production
vrai faux faux
Lecture recommandée: Javascript Object.propertyIsEnumerable ()








