OOP (Object-Oriented Programming) — Access Modifiers
Access modifiers control the visibility of class members (variables and methods). They determine what parts of the code can access certain properties or functions. Common access modifiers are Public, Private, and Protected. Public members can be accessed from anywhere, Private members are only accessible within the class, and Protected members can be accessed within the class and its subclasses. These modifiers help secure data and maintain proper encapsulation.
class Student {
#grade;
constructor(name, grade) {
this.name = name;
this.#grade = grade;
}
showGrade() {
console.log(this.name + ' has grade ' + this.#grade);
}
}
let s1 = new Student('Tom', 'A');
s1.showGrade();