9.7 Omitting descriptor properties

All properties of descriptors are optional. What happens when you omit a property depends on the operation.

9.7.1 Omitting descriptor properties when creating properties

When we create a new property via a descriptor, then omitting attributes means that their default values are used:

  1. const car = {};
  2. Object.defineProperty(
  3. car, 'color', {
  4. value: 'red',
  5. });
  6. assert.deepEqual(
  7. Object.getOwnPropertyDescriptor(car, 'color'),
  8. {
  9. value: 'red',
  10. writable: false,
  11. enumerable: false,
  12. configurable: false,
  13. });

9.7.2 Omitting descriptor properties when changing properties

If instead, we change an existing property, then omitting descriptor properties means that the corresponding attributes are not touched:

  1. const car = {
  2. color: 'yellow',
  3. };
  4. assert.deepEqual(
  5. Object.getOwnPropertyDescriptor(car, 'color'),
  6. {
  7. value: 'yellow',
  8. writable: true,
  9. enumerable: true,
  10. configurable: true,
  11. });
  12. Object.defineProperty(
  13. car, 'color', {
  14. value: 'pink',
  15. });
  16. assert.deepEqual(
  17. Object.getOwnPropertyDescriptor(car, 'color'),
  18. {
  19. value: 'pink',
  20. writable: true,
  21. enumerable: true,
  22. configurable: true,
  23. });