JavaScript attribute descriptor

Interpretation of attribute descriptors and their role

Any object in JavaScript has a set of properties, and each property has oneDescriptor.

This is an object that defines property behavior and its own properties.

Many object static methods interact with it. These methods include:

This is an example of an attribute descriptor object:

{
  value: 'Something'
}

This is the simplest one.valueIs the attribute value in the key definition. ThiskeyWhen defining this property in an object, define it as an object key:

{
  breed: {
    value: 'Siberian Husky'
  }
}

example:

const animal = {}
const dog = Object.create(animal, {
  breed: {
    value: 'Siberian Husky'
  }
});
console.log(dog.breed) //'Siberian Husky'

You can pass other attributes to define each different object attribute:

  • value: The value of the property
  • Writable: True can change the attribute
  • Configurable: If it is false, the attribute cannot be deleted, nor can any attribute be changed, except for its value
  • Countless: True if the attribute is enumerable
  • get: The getter function of the property, which is called when the property is read
  • put: The setter function of the property, which is called when the property is set to the value

writable,configurablewithenumerableSet the behavior of this property. They have boolean values, and by default, they are bothfalse.

example:

const animal = {}
const dog = Object.create(animal, {
  breed: {
    value: 'Siberian Husky',
    writable: false
  }
});
console.log(dog.breed) //'Siberian Husky'
dog.breed = 'Pug' //TypeError: Cannot assign to read only property 'breed' of object '#<Object>'

Download mine for freeJavaScript beginner's manual


More js tutorials: