a - getters and setters introduction Getters and Setters are special functions that are used to get and set the values of an object's properties. These functions allow you to control how properties are accessed and modified, which can be useful for encapsulation and validation. Getters and setters are powerful tools for controlling how properties are accessed and modified in JavaScript, providing a clean and maintainable way to work with object-oriented programming. A getter is a function that returns the value of a property. It is defined using the get keyword followed by the name of the property. A setter is a function that sets the value of a property. It is defined using the set keyword followed by the name of the property. Benefits of Using Getters and Setters: 1. Encapsulation: They allow you to hide the internal representation of an object's data. 2. Validation: You can add logic to ensure that only valid values are assigned to properties. 3. Computed Properties: You can define properties whose values are computed based on other properties.
b - Class getter A getter is a function that is used to retrieve the value of a property. When you try to read a property, JavaScript automatically calls the associated getter method if one exists. Getters are defined using the get keyword followed by the name of the property you want to define a getter for. The method should not take any parameters and should return the value of the property. In this example, the get area() method calculates the area of the circle based on its radius... The area property is a computed property because it does not store a value directly but instead computes it every time it is accessed. Best Practices: Use Getters for Accessing Properties: Getters should be used whenever you want to define custom behavior when a property is accessed. • Keep Getters Simple: While getters can include complex logic, it's generally best to keep them simple and focused on accessing the property. More complex logic may belong in separate methods.
c - Class setter A setter is a function that is used to set or modify the value of a property. When you try to assign a new value to a property, JavaScript automatically calls the associated setter method if one exists. Setters are defined using the set keyword followed by the name of the property you want to define a setter for. The method should take one parameter (the new value) and modify the internal state accordingly. In this example, the set method is used to define a setter for the _name property... When you assign a new value to person.name, JavaScript automatically calls the set name(value) method and updates the internal _name property accordingly. Best Practices: Use Setters for Modifying Properties: Setters should be used whenever you want to define custom behavior when a property is modified. Keep Setters Simple: While setters can include complex logic, it's generally best to keep them simple and focused on modifying the property. More complex logic may belong in separate methods.