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

Contenido del Curso

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

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

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

Tarea

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
toggle bottom row

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

Tarea

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
toggle bottom row

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

Tarea

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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.

Tarea

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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 2
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt