ES8 - OwnPropertyDescriptor

This tutorial covers ES8 Object.getOwnPropertyDescriptors tutorials, examples introduced to latest javascript features, in ES8, ES2017, with examples.

javascript Object getOwnPropertyDescriptors

getOwnPropertyDescriptors() method in object finds complete information of a property and returns property descriptors.

This will be useful for real shallow copy of an object cloning, Before ES8, Object will be copied using Object.assign() whichi is not suitable for complext object graph.

Syntax

Object.getOwnPropertyDescriptors(obj)

Parameters and return type - Input is an object - return property descriptiors of an given input object

Example

const myobject = {
 id:"1",
 name:"Tom",
};
const descriptors = Object.getOwnPropertyDescriptors(myobject);
console.log(descriptors);

And the output is

{
  id: {
    value: '1',
    writable: true,
    enumerable: true,
    configurable: true
  },
  name: {
    value: 'Tom',
    writable: true,
    enumerable: true,
    configurable: true
  }
}

Descriptors

console.log(descriptors.id.writable); //true
console.log(descriptors.id.value); //1
console.log(descriptors.id.get); // undefined
console.log(descriptors.id.set); // undefined
console.log(descriptors.id.configurable); // true
console.log(descriptors.id.enumerable); // true

Descriptors allowed for each property are as follows

  • writable - the property allows to modify or not
  • value - the value of an property key
  • get - returns the getter function for the property if present,else undefined
  • set - setter function of the property, if present, else undefined
  • configurable - true, can be changed and will be delete from an object,else false.
  • enumerable - if true, will be treate as enumerable property and shown during iteration

Important points - It returns the own properties of an object - It does not consider properties from Object.protototype inheritence

Support This will support in latest browsers To support in older browsers, enable the following plugins in your project - polyfill - babel plugin

Prev Next