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
}Example 1: Understanding Property Descriptors
✅ 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...inloopsObject.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: trueenumerable: trueconfigurable: true
Example 2: Advanced Property Descriptor Examples
🔧 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 madetrueagain - If
writable: false, cannot maketrueifconfigurable: 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
📊 Object Methods Comparison
| Method | Effect on Properties | Can Add Properties | Can Delete Properties | Can 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) | Depends | Depends |
Example 4: Practical Use Cases
🎯 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
Exercise: Implement Property Descriptors
💡 Best Practices Summary
- Use property descriptors for intentional design, not just because you can
- Prefer
Object.freeze()for complete immutability - Use
enumerable: falseto 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
- What are the four property descriptor attributes?
- What's the difference between
writable: falseandconfigurable: false? - How do getters and setters work with property descriptors?
- What happens when you try to modify a non-writable property?
- How do
Object.freeze(),Object.seal(), andObject.preventExtensions()differ? - How can you make a property completely private (inaccessible)?
- What are the performance implications of using property descriptors?
- How would you implement observable properties using descriptors?
- What's the difference between
Object.keys()andObject.getOwnPropertyNames()? - How can property descriptors help with security?