Conteúdo do Curso
Object-Oriented Programming in JavaScript
Object-Oriented Programming in JavaScript
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.
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);
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
:
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);
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.
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.
Obrigado pelo seu feedback!
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.
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);
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
:
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);
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.
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.
Obrigado pelo seu feedback!