Object Property Descriptors

🔍 What are Property Descriptors?

Property descriptors are metadata that define how properties behave in JavaScript objects. They control whether a property can be changed, deleted, enumerated, or configured.

Property Descriptor Structure
Data Descriptor (for regular properties)
{
  value: any,           // Property value
  writable: boolean,    // Can be changed
  enumerable: boolean,  // Shows in for...in
  configurable: boolean // Can be deleted/reconfigured
}
Accessor Descriptor (for getters/setters)
{
  get: function,        // Getter function
  set: function,        // Setter function
  enumerable: boolean,
  configurable: boolean
}
Note: A descriptor cannot be both data and accessor descriptor. It must be one or the other.

Example 1: Understanding Property Descriptors

JavaScript Editor
✅ Property Descriptor Flags
writable

If false, the property value cannot be changed.

obj.prop = value will fail silently in non-strict mode.
enumerable

If false, the property doesn't show up in:

  • for...in loops
  • Object.keys()
  • JSON.stringify()
configurable

If false, you cannot:

  • Delete the property
  • Change its descriptor
  • Change its type (data ↔ accessor)
Default Values

When creating properties normally:

  • writable: true
  • enumerable: true
  • configurable: true

Example 2: Advanced Property Descriptor Examples

JavaScript Editor
🔧 Common Patterns
  • Read-only properties: Set writable: false
  • Hidden properties: Set enumerable: false
  • Locked properties: Set configurable: false
  • Computed properties: Use getters without setters
  • Validation: Use setters with validation logic
  • Observables: Combine with Proxy or setters
⚠️ Important Notes
  • Once configurable: false, cannot be made true again
  • If writable: false, cannot make true if configurable: false
  • Non-enumerable properties still accessible directly
  • Use Object.getOwnPropertyNames() to get all properties (including non-enumerable)
  • Strict mode throws errors on violations

Example 3: Multiple Properties and Object Methods

JavaScript Editor
📊 Object Methods Comparison
MethodEffect on PropertiesCan Add PropertiesCan Delete PropertiesCan Modify Properties
Object.preventExtensions()Prevents new properties❌ No✅ Yes✅ Yes
Object.seal()Prevents add/delete, makes configurable: false❌ No❌ No✅ Yes
Object.freeze()Prevents all changes, makes writable: false❌ No❌ No❌ No
Object.defineProperty()Configure individual property✅ Yes (if new)DependsDepends

Example 4: Practical Use Cases

JavaScript Editor
🎯 Real-World Applications
1. Configuration Objects

Create immutable configuration objects that cannot be accidentally modified.

2. API Responses

Make certain response fields read-only to prevent client-side modification.

3. Library/Plugin Development

Expose public API while keeping internal properties hidden.

4. Form Validation

Create objects with built-in validation for form data.

5. State Management

Create observable state objects for reactive programming.

6. Security

Hide sensitive data from serialization and iteration.

Example 5: Performance and Best Practices

JavaScript Editor
Exercise: Implement Property Descriptors
JavaScript Editor
💡 Best Practices Summary
  • Use property descriptors for intentional design, not just because you can
  • Prefer Object.freeze() for complete immutability
  • Use enumerable: false to hide implementation details
  • Combine with Proxies for advanced reactivity patterns
  • Avoid in performance-critical code (use sparingly)
  • Document why properties are configured a certain way
  • Consider using TypeScript for compile-time property safety
  • Test descriptor behavior in strict mode
🎯 Interview Questions on Property Descriptors
  1. What are the four property descriptor attributes?
  2. What's the difference between writable: false and configurable: false?
  3. How do getters and setters work with property descriptors?
  4. What happens when you try to modify a non-writable property?
  5. How do Object.freeze(), Object.seal(), and Object.preventExtensions() differ?
  6. How can you make a property completely private (inaccessible)?
  7. What are the performance implications of using property descriptors?
  8. How would you implement observable properties using descriptors?
  9. What's the difference between Object.keys() and Object.getOwnPropertyNames()?
  10. How can property descriptors help with security?