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

Зміст курсу

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

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

bookPrivate Modifier

Encapsulation is a process of control and validating the behavior of the class and class intances. Our Student has name and age attributes, and the user can access them from any place of program, and, of course, change it. Seems that it is not the best approach – you created a student with the name Mary, and suddenly you want to change it directly, or you want to change the age, and you assign it with some non-correct value, for example, -12.

That’s why there is an access modifier private: to forbid access to the attributes or method. You can access it only inside the class(for example, inside the method’s body). By default, all methods and attributes are public: you can access them anywhere in your program.

To make properties private, use #.

12345678910111213141516171819202122
class Student{ #name; #university; #age; #gpa; constructor(name, age){ this.#name = name; this.#age = age; console.log('Student created'); } sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.#age += 1; console.log('Today is my birthday'); } } student = new Student('Robert', 22);
copy

Завдання

Make age and name attributes public and let other attributes be private. Implement the constructor and then create a Student object and output the name of this object.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
toggle bottom row

bookPrivate Modifier

Encapsulation is a process of control and validating the behavior of the class and class intances. Our Student has name and age attributes, and the user can access them from any place of program, and, of course, change it. Seems that it is not the best approach – you created a student with the name Mary, and suddenly you want to change it directly, or you want to change the age, and you assign it with some non-correct value, for example, -12.

That’s why there is an access modifier private: to forbid access to the attributes or method. You can access it only inside the class(for example, inside the method’s body). By default, all methods and attributes are public: you can access them anywhere in your program.

To make properties private, use #.

12345678910111213141516171819202122
class Student{ #name; #university; #age; #gpa; constructor(name, age){ this.#name = name; this.#age = age; console.log('Student created'); } sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.#age += 1; console.log('Today is my birthday'); } } student = new Student('Robert', 22);
copy

Завдання

Make age and name attributes public and let other attributes be private. Implement the constructor and then create a Student object and output the name of this object.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
toggle bottom row

bookPrivate Modifier

Encapsulation is a process of control and validating the behavior of the class and class intances. Our Student has name and age attributes, and the user can access them from any place of program, and, of course, change it. Seems that it is not the best approach – you created a student with the name Mary, and suddenly you want to change it directly, or you want to change the age, and you assign it with some non-correct value, for example, -12.

That’s why there is an access modifier private: to forbid access to the attributes or method. You can access it only inside the class(for example, inside the method’s body). By default, all methods and attributes are public: you can access them anywhere in your program.

To make properties private, use #.

12345678910111213141516171819202122
class Student{ #name; #university; #age; #gpa; constructor(name, age){ this.#name = name; this.#age = age; console.log('Student created'); } sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.#age += 1; console.log('Today is my birthday'); } } student = new Student('Robert', 22);
copy

Завдання

Make age and name attributes public and let other attributes be private. Implement the constructor and then create a Student object and output the name of this object.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Encapsulation is a process of control and validating the behavior of the class and class intances. Our Student has name and age attributes, and the user can access them from any place of program, and, of course, change it. Seems that it is not the best approach – you created a student with the name Mary, and suddenly you want to change it directly, or you want to change the age, and you assign it with some non-correct value, for example, -12.

That’s why there is an access modifier private: to forbid access to the attributes or method. You can access it only inside the class(for example, inside the method’s body). By default, all methods and attributes are public: you can access them anywhere in your program.

To make properties private, use #.

12345678910111213141516171819202122
class Student{ #name; #university; #age; #gpa; constructor(name, age){ this.#name = name; this.#age = age; console.log('Student created'); } sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.#age += 1; console.log('Today is my birthday'); } } student = new Student('Robert', 22);
copy

Завдання

Make age and name attributes public and let other attributes be private. Implement the constructor and then create a Student object and output the name of this object.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 2. Розділ 1
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt