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

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.

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);
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:

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);
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.

Oppgave

Swipe to start coding

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.

Løsning

class Student{
#name
#age
#gpa

constructor(name, age){
this.#name = name
this.#age = age
console.log('Student created');
}

set gpa(value){
if (value >= 1.0 && value <= 4.0) { this.#gpa = value }
else { this.gpa = 1.0 }
}
get gpa() {
return this.#gpa;
}
}

student = new Student('Robert', 22, 3.2);
student.gpa = -6;
console.log('Current GPA is:', student.gpa);
student.gpa = 3.4;
console.log('Current GPA is:', student.gpa);

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
single

single

class Student{
#name;
#age;
#gpa;

constructor(name, age){
this.#name = name;
this.#age = age;
console.log('Student created');
}
}

student = new Student('Robert', 22);
student.gpa = -6;
console.log('Current GPA is:', student.gpa);
student.gpa = 3.4;
console.log('Current GPA is:', student.gpa);

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

We use cookies to make your experience better!
some-alt