Object properties and attributes

Each object has a few properties. Each property, in turn, has a key and attributes. A property's state is stored in these attributes. All properties have the following attributes:

  • Enumerable (boolean): This indicates if you can enumerate the properties of the object. System properties are non-enumerable while user properties are enumerable. Unless there is a strong reason, this property should remain untouched.
  • Configurable(boolean): If this attribute is false, the property cannot be deleted or edited (it cannot change any of its attribute).

You can use the Object.getOwnPropertyDescriptor() method to retrieve an object's own properties:

    let obj = { 
      age: 25 
    } 
    console.log(Object.getOwnPropertyDescriptor(obj, 'age')); 
    //{"value":25,"writable":true,"enumerable":true,"configurable":true} 

Meanwhile, the property can be defined using the Object.defineProperty() method:

    let obj = { 
      age: 25 
    } 
    Object.defineProperty(obj, 'age', { configurable: false }) 
    console.log(Object.getOwnPropertyDescriptor(obj, 'age')); 
    //{"value":25,"writable":true,"enumerable":true,"configurable":false} 

Though you would never use these methods, it is important to understand object properties and attributes. In the next section, we will discuss how some of the object methods are used in context of some of these properties.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.186.164