Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Setters in Classes | Inheritance and Encapsulation
JavaScript Classes and OOP Foundations

bookSetters 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.

1234567891011121314151617181920212223242526
class 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
copy
question mark

Which statements about setters in JavaScript classes are correct based on the chapter content

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 6

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 6.25

bookSetters in Classes

Scorri per mostrare il menu

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.

1234567891011121314151617181920212223242526
class 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
copy
question mark

Which statements about setters in JavaScript classes are correct based on the chapter content

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 6
some-alt