Setters in Classes
When you want to control how a property is set on an object, you can use a setter method in a JavaScript class. Setters use the set keyword followed by a method name, and are designed to execute code whenever a property is assigned a new value. This lets you validate, transform, or restrict values before they are actually stored. Setters are especially useful for encapsulation: they allow you to expose a property for assignment, but keep tight control over what values are accepted.
The set syntax looks similar to defining a method, but with the set keyword in front. Inside the setter, you typically store the value in a private or internal property (often with a leading underscore or, in later chapters, a private field). You can also add checks to ensure the value meets certain criteria before saving it, or transform it as needed.
1234567891011121314151617181920212223242526class Product { constructor(name, price) { this.name = name; this._price = price; // Internal property } get price() { return this._price; } set price(value) { // Validate that price is a positive number if (typeof value !== "number" || value < 0) { throw new Error("Price must be a non-negative number"); } this._price = value; } } const item = new Product("Book", 15); console.log(item.price); // 15 item.price = 25; // Uses the setter console.log(item.price); // 25 // item.price = -10; // Would throw an error
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 6.25
Setters in Classes
Stryg for at vise menuen
When you want to control how a property is set on an object, you can use a setter method in a JavaScript class. Setters use the set keyword followed by a method name, and are designed to execute code whenever a property is assigned a new value. This lets you validate, transform, or restrict values before they are actually stored. Setters are especially useful for encapsulation: they allow you to expose a property for assignment, but keep tight control over what values are accepted.
The set syntax looks similar to defining a method, but with the set keyword in front. Inside the setter, you typically store the value in a private or internal property (often with a leading underscore or, in later chapters, a private field). You can also add checks to ensure the value meets certain criteria before saving it, or transform it as needed.
1234567891011121314151617181920212223242526class Product { constructor(name, price) { this.name = name; this._price = price; // Internal property } get price() { return this._price; } set price(value) { // Validate that price is a positive number if (typeof value !== "number" || value < 0) { throw new Error("Price must be a non-negative number"); } this._price = value; } } const item = new Product("Book", 15); console.log(item.price); // 15 item.price = 25; // Uses the setter console.log(item.price); // 25 // item.price = -10; // Would throw an error
Tak for dine kommentarer!