🟑 MediumObjectsπŸ“– Theory Question

What are property descriptors and property flags (writable, enumerable, configurable)?

πŸ’‘

Hint

Every property has a descriptor controlling whether it can be changed, seen, or deleted

Full Answer

Every object property has a descriptor with 3 flags:

  • writable β€” can the value be changed?
  • enumerable β€” does it show up in for...in / Object.keys()?
  • configurable β€” can the descriptor be changed? Can the property be deleted?
const obj = {};

Object.defineProperty(obj, 'ID', {
  value: 42,
  writable: false,    // read-only
  enumerable: false,  // hidden from loops
  configurable: false // can't redefine or delete
});

obj.ID = 99;              // silently fails (TypeError in strict)
console.log(obj.ID);      // 42

Object.keys(obj);         // [] β€” ID is non-enumerable
delete obj.ID;            // false β€” non-configurable

// View a property's descriptor
Object.getOwnPropertyDescriptor(obj, 'ID');
// { value: 42, writable: false, enumerable: false, configurable: false }

// Regular property defaults:
// { value: ..., writable: true, enumerable: true, configurable: true }
πŸ’‘ Object.freeze() sets writable + configurable to false for all props. Object.seal() sets configurable to false but keeps writable. Both use property descriptors under the hood.

More Objects Questions

🟒 EasyHow does prototypal inheritance work in JavaScript?β†’πŸŸ’ EasyWhat is the difference between shallow copy and deep copy?β†’πŸŸ’ EasyWhat are getters and setters in JavaScript?β†’πŸŸ’ EasyWhat is the difference between Object.freeze() and Object.seal()?β†’

Practice this in a timed sprint β†’

5 free questions, no signup required

⚑ Start Sprint