Oggetto JavaScript getOwnPropertyNames ()

Il metodo JavaScript Object.getOwnPropertyNames () restituisce un array di tutte le proprietà trovate in un dato oggetto.

La sintassi del getOwnPropertyNames()metodo è:

 Object.getOwnPropertyNames(obj)

Il getOwnPropertyNames()metodo, essendo un metodo statico, viene chiamato utilizzando il Objectnome della classe.

getOwnPropertyNames () Parametri

Il getOwnPropertyNames()metodo comprende:

  • obj - L'oggetto le cui proprietà enumerabili e non enumerabili devono essere restituite.

Valore restituito da getOwnPropertyNames ()

  • Restituisce un array di stringhe che corrisponde alle proprietà trovate direttamente nell'oggetto dato.

Nota: Object.getOwnPropertyNames() restituisce tutte le proprietà proprie dell'oggetto mentre Object.keys()restituisce tutte le proprietà proprie enumerabili.

Esempio: utilizzo di 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' )

Produzione

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

Letture consigliate: Javascript Object.hasOwnProperty ()

Articoli interessanti...