Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Setters in Classes | Inheritance and Encapsulation
Quizzes & Challenges
Quizzes
Challenges
/
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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 6

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookSetters in Classes

Glissez pour afficher le 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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 6
some-alt