In this tutorial, you'll learn about getters and setters in JavaScript, which are special methods used to control access to an object's properties. Understanding how to use these keywords is crucial for encapsulating data and ensuring that your code adheres to best practices.
Getters and setters are essential tools in JavaScript for managing the way properties of an object are accessed and modified. They allow you to enforce rules, validate data, and maintain the integrity of your objects. By using getters and setters, you can encapsulate the internal state of an object and provide a controlled interface for interacting with it.
A getter is a method that allows you to retrieve the value of a property. It is defined using the get keyword followed by the name of the property. When you access the property, the getter method is automatically invoked.
A setter is a method that allows you to set the value of a property. It is defined using the set keyword followed by the name of the property. When you assign a value to the property, the setter method is automatically invoked.
Here's the basic syntax for defining getters and setters in a class:
1class Person {2constructor(name) {3this._name = name;4}56get name() {7return this._name.toUpperCase();8}910set name(newName) {11if (newName.length > 2) {12this._name = newName;13} else {14console.log('Name must be at least 3 characters long.');15}16}17}
_name property.get name): Returns the name in uppercase when accessed.set name): Sets a new name only if it is longer than 2 characters.Let's create a practical example to see how getters and setters work. We'll define a Car class with properties for make, model, and year. We'll use getters and setters to control access to these properties.
1// car.js2class Car {3constructor(make, model, year) {4this._make = make;5this._model = model;6this._year = year;7}89get make() {10return this._make;11}1213set make(newMake) {14if (newMake.length > 0) {15this._make = newMake;16} else {17console.log('Make must not be empty.');18}19}2021get model() {22return this._model;23}2425set model(newModel) {26if (newModel.length > 0) {27this._model = newModel;28} else {29console.log('Model must not be empty.');30}31}3233get year() {34return this._year;35}3637set year(newYear) {38if (newYear >= 1900 && newYear <= new Date().getFullYear()) {39this._year = newYear;40} else {41console.log('Year must be between 1900 and the current year.');42}43}44}4546// main.js47const myCar = new Car('Toyota', 'Corolla', 2020);4849console.log(myCar.make); // Output: Toyota50myCar.make = 'Honda';51console.log(myCar.make); // Output: Honda5253console.log(myCar.model); // Output: Corolla54myCar.model = '';55console.log(myCar.model); // Output: Model must not be empty. Corolla5657console.log(myCar.year); // Output: 202058myCar.year = 1899;59console.log(myCar.year); // Output: Year must be between 1900 and the current year. 2020
| Concept | Description |
|---|---|
| Getters | Methods that retrieve property values. |
| Setters | Methods that set property values with validation. |
| Encapsulation | Hiding the internal state of an object and providing controlled access through getters and setters. |
In the next tutorial, we'll explore JavaScript Sets. A Set is a collection of unique values, which can be particularly useful for managing distinct items in your applications. Understanding how to use sets will enhance your ability to handle data efficiently.
Stay tuned!