Public Instance Setters in JavaScript Classes
In this article, we will learn about public instance setters in JavaScript classes. Public instance setters are methods that allow us to set the value of a public instance property of a class. Public instance properties are properties that belong to each instance of a class, and can be accessed and modified by anyone.
Why Use Public Instance Setters?
Public instance setters are useful for several reasons:
- They can provide validation and error handling for the property value, ensuring that it meets certain criteria or conditions.
- They can perform additional actions or side effects when the property value is changed, such as updating other properties or triggering events.
- They can make the code more readable and consistent, by following the convention of using a method name that starts with
set
and ends with the property name.
How to Define Public Instance Setters?
We can define public instance setters in JavaScript classes using the set
keyword, followed by the method name and a parameter that represents the new value of the property. The method name should match the property name, or be a variation of it. For example, if the property name is name
, the setter name can be setName
or name
. The parameter name can be anything, but it is common to use a name that indicates the new value, such as newValue
or value
.
The syntax of defining a public instance setter is as follows:
class ClassName {
// Define the property with an initial value
propertyName = initialValue;
// Define the setter with the set keyword
set setterName(newValue) {
// Validate and assign the new value to the property
this.propertyName = newValue;
// Perform any additional actions or side effects
// ...
}
}
Here is an example of defining a public instance setter for a name
property in a Person
class:
class Person {
// Define the name property with an initial value
name = "Anonymous";
// Define the name setter with the set keyword
set name(newValue) {
// Validate the new value, and throw an error if it is not a string
if (typeof newValue !== "string") {
throw new TypeError("Name must be a string");
}
// Assign the new value to the name property
this.name = newValue;
// Log a message to the console
console.log(`The name of this person is now ${this.name}`);
}
}
How to Use Public Instance Setters?
We can use public instance setters in JavaScript classes by assigning a new value to the property, using the dot notation or the bracket notation. The setter method will be automatically invoked, and the new value will be passed as the parameter. For example, if we have a person
instance of the Person
class, we can use the name
setter as follows:
// Using the dot notation
person.name = "Alice";
// Using the bracket notation
person["name"] = "Bob";
Both statements will invoke the name
setter method, and assign the new value to the name
property. They will also log a message to the console, as defined in the setter method.
Conclusion
In this article, we have learned about public instance setters in JavaScript classes. Public instance setters are methods that allow us to set the value of a public instance property of a class. They can provide validation, error handling, and additional actions for the property value. We can define public instance setters using the set
keyword, and use them by assigning a new value to the property. Public instance setters can make our code more readable and consistent, and enhance the functionality of our classes.