JavaScript Getters and Setters
Introduction to Getters and Setters
Getters and setters in JavaScript allow you to control access to object properties. They enable encapsulation by providing a way to define how properties are retrieved and updated.
Getters and Setters in Classes
class Course {
#name = "Default"; // Private field
constructor(name, teacher, videos) {
this.#name = name;
this.teacher = teacher;
this.videos = videos;
}
print() {
console.log(this);
}
// Getter for the name property
get name() {
return this.#name.toUpperCase();
}
// Setter for the name property
set name(value) {
this.#name = value; // Encapsulated logic for setting the name
}
}
const js = new Course("JavaScript", "DeathCode", 40);
js.name = "Advanced JavaScript"; // Using the setter
console.log(js.name); // Using the getter
Explanation:
#name
: A private field accessible only within the class.get name()
: A getter method that returns the name in uppercase.set name(value)
: A setter method that updates the private field#name
.
Getters and Setters in Constructor Functions
function User(name, username) {
this._name = name; // Internal property
this._username = username;
Object.defineProperty(this, 'name', {
get: function () {
return this._name.toUpperCase();
},
set: function (value) {
this._name = value; // Encapsulated logic for setting the name
}
});
}
const user = new User("death", "deathcode");
console.log(user.name); // Using the getter
user.name = "newName"; // Using the setter
console.log(user.name);
Explanation:
Object.defineProperty
: Used to define getters and setters on object properties.get
: Retrieves the value of the property with additional logic.set
: Updates the property value with additional logic.
Behind the Scenes: How Getters and Setters Work
In classes, getters and setters are syntactic sugar over the Object.defineProperty
mechanism. When you define a getter or setter, the property is automatically added to the object's prototype.
Key Features and Benefits
- Encapsulation: Control how properties are accessed and modified.
- Validation: Add custom logic for property updates.
- Readability: Provide intuitive interfaces for object properties.
Best Practices
- Use getters and setters for encapsulation and validation.
- Avoid excessive logic in getters and setters to maintain performance.
- Prefer private fields (e.g.,
#name
) for truly private data in classes.
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 CodeCurrently Playing
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeWatch
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