Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Get and Set | Encapsulation
Object-Oriented Programming in JavaScript
course content

Conteúdo do Curso

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

1. Classes & Objects
2. Encapsulation
3. Static
4. Inheritance

book
Get and Set

For direct access to the class properties, you can use two access methods - get and set.

get lets you read only some private data, but not change it.

set provides an opportunity to change the value of some attributes.

This approach also demonstrates the encapsulation principle: some attributes you can only read, but not modify, and some attributes you can modify only.

12345678910111213141516171819
class Student{ #name; #university; #age; #gpa; constructor(name, age){ this.#name = name; this.#age = age; console.log('Student created'); } get name(){ return this.#name; } } student = new Student('Robert', 22); console.log(student.name);
copy

In other words, you can not access the private attribute name, but after adding get(), you'll have an opportunity to extract the value of student.name, but not to change it. But if you add set(value) to this attribute, you may change the student.name.

You can ask: why name attribute is private if we can modify it and read it using get and set? The reason is that inside set(value) method, you can validate the value before assigning the attribute. Look at the example with age:

123456789101112131415161718192021222324
class Student{ #name; #age; constructor(name, age){ this.#name = name; this.#age = age; console.log('Student created'); } set age(value){ if (value >= 0 && value <= 100) { this.#age = value } else { this.#age = 18 }; } get age() { return this.#age; } } student = new Student('Robert', 22); student.age = 120 console.log('Current age is:', student.age); student.age = 64; console.log('Current age is:', student.age);
copy

First time, age has wrong value, so by default student.age will be 18. In the second case, when we assign age with 64, everything is valid, so student.age is 64 now.

This way, you won't set some wrong values in the age attribute.

You can use either get or set method according to your aim.

Tarefa
test

Swipe to show code editor

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
toggle bottom row

book
Get and Set

For direct access to the class properties, you can use two access methods - get and set.

get lets you read only some private data, but not change it.

set provides an opportunity to change the value of some attributes.

This approach also demonstrates the encapsulation principle: some attributes you can only read, but not modify, and some attributes you can modify only.

12345678910111213141516171819
class Student{ #name; #university; #age; #gpa; constructor(name, age){ this.#name = name; this.#age = age; console.log('Student created'); } get name(){ return this.#name; } } student = new Student('Robert', 22); console.log(student.name);
copy

In other words, you can not access the private attribute name, but after adding get(), you'll have an opportunity to extract the value of student.name, but not to change it. But if you add set(value) to this attribute, you may change the student.name.

You can ask: why name attribute is private if we can modify it and read it using get and set? The reason is that inside set(value) method, you can validate the value before assigning the attribute. Look at the example with age:

123456789101112131415161718192021222324
class Student{ #name; #age; constructor(name, age){ this.#name = name; this.#age = age; console.log('Student created'); } set age(value){ if (value >= 0 && value <= 100) { this.#age = value } else { this.#age = 18 }; } get age() { return this.#age; } } student = new Student('Robert', 22); student.age = 120 console.log('Current age is:', student.age); student.age = 64; console.log('Current age is:', student.age);
copy

First time, age has wrong value, so by default student.age will be 18. In the second case, when we assign age with 64, everything is valid, so student.age is 64 now.

This way, you won't set some wrong values in the age attribute.

You can use either get or set method according to your aim.

Tarefa
test

Swipe to show code editor

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt