JavaScript Object Descriptors
Introduction
Object descriptors in JavaScript provide detailed control over object properties. Using Object.defineProperty
, you can define and modify the behavior of individual properties.
Key Descriptor Properties
Each property descriptor can have the following key-value pairs:
- writable: Determines if the property value can be changed (
true
orfalse
). - enumerable: Determines if the property appears during enumeration (
true
orfalse
). - configurable: Determines if the property descriptor can be changed or deleted (
true
orfalse
). - value: The value of the property.
- get: A function to retrieve the property value.
- set: A function to set the property value.
Example Code
const user = {
name: "death",
name2: "death",
name3: "death",
};
// Viewing the property descriptor for 'name'
console.log(Object.getOwnPropertyDescriptor(user, 'name'));
// Modifying the 'name' property descriptor
Object.defineProperty(user, 'name', {
writable: false,
enumerable: false,
configurable: false
});
// Attempting to change 'name' (won't work due to writable: false)
user.name = "pop";
// Viewing the descriptor of Math.PI
console.log(Object.getOwnPropertyDescriptor(Math, 'PI'));
// Keys of 'user' (won't include 'name' due to enumerable: false)
console.log(Object.keys(user));
// Accessing 'name'
console.log(user.name);
// Attempting to modify 'name' descriptor (won't work due to configurable: false)
try {
Object.defineProperty(user, 'name', {
configurable: true
});
} catch (error) {
console.error("Error modifying 'name' descriptor:", error);
}
// Deleting 'name' (won't work due to configurable: false)
delete user.name;
console.log(user.name);
Explanation
- The
writable
property controls whether the value can be changed. - The
enumerable
property determines if the property is visible in enumeration methods likeObject.keys
. - The
configurable
property controls if the descriptor can be modified or deleted.
Tips and Tricks
- Use
Object.getOwnPropertyDescriptor
to inspect property attributes. - Set
enumerable: false
for sensitive properties to hide them during enumeration. - Set
writable: false
for constants or read-only properties. - Use
configurable: false
to lock down critical properties.
Advanced Use Cases
Descriptors can also be used with getters and setters for advanced property control:
Object.defineProperty(user, 'greeting', {
get: function () {
return `Hello, ${this.name}`;
},
set: function (value) {
this.name = value;
}
});
console.log(user.greeting); // Outputs: "Hello, death"
user.greeting = "newName";
console.log(user.name); // Outputs: "newName"
Playlist of JS-Journey (JavaScript)
1. Introduction to JavaScript - What, Why, and How? | JavaScript Journey | In Hindi | Death CodeWatch
2. Variables & Comments in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
3. Understanding Data Types in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
4. Master Type Conversion & Reference Data Types π | JavaScript Journey | In Hindi | Death CodeWatch
5. Mastering Operators in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
6. Mastering Numbers & Math Functions π | JavaScript Journey | In Hindi | Death CodeWatch
7. Strings and String Methods in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
8. Mastering Dates and Date Methods in JavaScript β³ | JavaScript Journey | In Hindi | Death CodeWatch
9. JavaScript Arrays & Array Methods Explained β³ | JavaScript Journey | In Hindi | Death CodeWatch
10. Objects and Object Methods Explained π | JavaScript Journey | In Hindi | Death CodeWatch
11. Conditional Statements | If, Switch & Ternary Operator | JavaScript Journey | In Hindi | Death CodeWatch
12. Mastering Loops (for, while, do-while) | JavaScript Journey | In Hindi | Death CodeWatch
13. Understanding Functions | JavaScript Journey | In Hindi | Death CodeWatch
14. HOFs, Callback Functions, Arrow Functions & More! π | JavaScript Journey | In Hindi | Death CodeWatch
15. Mastering Scope & Hoisting Explained in Hindi π₯ | JavaScript Journey | In Hindi | Death CodeWatch
16. Map, Filter, Reduce & forEach Explainedπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
17. Understanding this Keyword, Function Context & IIFEπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
18. Dive into Call Stack & JavaScript Execution Contextπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
19. Master Async Await & Event Loop π₯ | JavaScript Journey | In Hindi | Death CodeWatch
20. Promises in JavaScript Made Easy π | JavaScript Journey | In Hindi | Death CodeWatch
21. Fetch Function in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
22. JavaScript OOP Basics π | JavaScript Journey | In Hindi | Death CodeWatch
23. Mastering Prototypes & Inheritance in JS π | JavaScript Journey | In Hindi | Death CodeWatch
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeWatch
25. Mastering Getter and Setter (Why & How to use them?) π | JavaScript Journey | In Hindi | Death CodeWatch
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeCurrently Playing
27. DOM Basics π | JavaScript Journey | In Hindi | Death CodeWatch
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeWatch
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch