revision:
The Object.getOwnPropertyDescriptors() static method returns all own property descriptors of a given object.
Object.getOwnPropertyDescriptors(obj)
Parameters:
obj : required. The object for which to get all own property descriptors.
const object1 = { property1: 42 }; const descriptors1 = Object.getOwnPropertyDescriptors(object1); console.log(descriptors1.property1.writable); // Expected output: true console.log(descriptors1.property1.value); // Expected output: 42
<div> <p id="own-1"></p> <p id="own-2"></p> <p id="own-3"></p> <p id="own-4"></p> <p id="own-5"></p> <p id="own-6"></p> </div> <script> // object with get and set properties const gator = { name: 'Ben', type: 'reptilian', get fullName(){ return `${this.name}${this.type}`; }, set gatorName(name){ this.name = name; } }; console.log(gator) // {name: 'Ben', type: 'reptilian',} console.log(gator.fullName) // 'Benreptilian' document.getElementById("own-1").innerHTML = " properties of 'gator' : " + JSON.stringify(gator); document.getElementById("own-2").innerHTML = " properties of 'gator' : " + gator.fullName; const cayman = Object.assign({}, gator); console.log(cayman) // {name: 'Ben', type: 'reptilian', fullName: Benreptilian, gatorName: undef document.getElementById("own-3").innerHTML = " properties of 'cayman' : " + JSON.stringify(cayman); const crocodilian = Object.defineProperties({}, Object.getOwnPropertyDescriptors(gator)); console.log(Object.getOwnPropertyDescriptors(gator)); console.log(Object.getOwnPropertyDescriptors(cayman)); console.log(Object.getOwnPropertyDescriptors(crocodilian)); document.getElementById("own-4").innerHTML = " properties of 'gator' : " + JSON.stringify(Object.getOwnPropertyDescriptors(gator)); document.getElementById("own-5").innerHTML = " properties of 'cayman' : " + JSON.stringify(Object.getOwnPropertyDescriptors(cayman)); document.getElementById("own-6").innerHTML = " properties of 'crocodilian' : " + JSON.stringify(Object.getOwnPropertyDescriptors(crocodilian)); </script>
<div> <p id="own-7"></p> <p id="own-8"></p> <p id="own-9"></p> <p id="own-10"></p> <p id="own-11"></p> </div> <script> const ninja1 = { prop1: "NinjasforNinjas" } const ninja2 = { prop2: "a good Platform", prop3: "and Computer science portal" } const descriptor1 = Object.getOwnPropertyDescriptors(ninja1); const descriptor2 = Object.getOwnPropertyDescriptors(ninja2); console.log(descriptor1.prop1.enumerable); console.log(descriptor2.prop2.enumerable); console.log(descriptor1.prop1.value); console.log(descriptor2.prop2.value); console.log(descriptor2.prop3.value); document.getElementById("own-7").innerHTML = "descriptor1.prop1.enumerable : " + descriptor1.prop1.enumerable; document.getElementById("own-8").innerHTML = "descriptor2.prop2.enumerable : " + descriptor2.prop2.enumerable; document.getElementById("own-9").innerHTML = "descriptor1.prop1.value : " + descriptor1.prop1.value; document.getElementById("own-10").innerHTML = "descriptor2.prop2.value : " + descriptor2.prop2.value; document.getElementById("own-11").innerHTML = "descriptor2.prop3.enumerable : " + descriptor2.prop3.value; </script>