Objet JavaScript getOwnPropertyNames ()

La méthode JavaScript Object.getOwnPropertyNames () renvoie un tableau de toutes les propriétés trouvées dans un objet donné.

La syntaxe de la getOwnPropertyNames()méthode est:

 Object.getOwnPropertyNames(obj)

La getOwnPropertyNames()méthode, étant une méthode statique, est appelée en utilisant le Objectnom de la classe.

Paramètres getOwnPropertyNames ()

La getOwnPropertyNames()méthode prend en compte:

  • obj - Objet dont les propriétés énumérables et non énumérables doivent être renvoyées.

Valeur renvoyée par getOwnPropertyNames ()

  • Renvoie un tableau de chaînes qui correspond aux propriétés trouvées directement dans l'objet donné.

Remarque: Object.getOwnPropertyNames() renvoie toutes les propriétés propres de l'objet tandis que Object.keys()renvoie toutes les propriétés propres énumérables.

Exemple: utilisation de getOwnPropertyNames ()

 // array object let arr = ("a", "b", "c"); console.log(Object.getOwnPropertyNames(arr)); // ( '0', '1', '2', 'length' ) // array-like objects let obj = ( 65: "A", 66: "B", 67: "C" ); console.log(Object.getOwnPropertyNames(obj)); // ( '65', '66', '67' ) // non-enumerable properties are also returned let obj1 = Object.create( (), ( getValue: ( value: function () ( return this.value; ), enumerable: false, ), ) ); obj1.value = 45; console.log(Object.getOwnPropertyNames(obj1)); // ( 'getValue', 'value' )

Production

 ('0', '1', '2', 'longueur') ('65', '66', '67') ('getValue', 'value')

Lecture recommandée: Javascript Object.hasOwnProperty ()

Articles intéressants...