Private and Protected Properties & Methods in JavaScript

Private and Protected Properties & Methods in JavaScript

Private and Protected Properties & Methods in JavaScript

In JavaScript, private and protected properties/methods help encapsulate class data, preventing direct modification from outside the class.

šŸ”¹ Private Properties and Methods (#)

Private properties and methods can only be accessed inside the class where they are defined.

Example: Private Property & Method (#)

class User { #password; // Private property constructor(name, password) { this.name = name; this.#password = password; } #encryptPassword() { // Private method return this.#password.split("").reverse().join(""); } getEncryptedPassword() { return this.#encryptPassword(); // Allowed inside the class } } const user = new User("Alice", "secure123"); console.log(user.name); // ✅ Output: Alice // console.log(user.#password); ❌ SyntaxError: Private field '#password' must be declared in an enclosing class // console.log(user.#encryptPassword()); ❌ Error: Private method cannot be accessed console.log(user.getEncryptedPassword()); // ✅ Works: 321eruces

#password and #encryptPassword() are private, so they cannot be accessed from outside the class.
getEncryptedPassword() provides a controlled way to access the encrypted password.

šŸ”¹ Protected Properties and Methods (_ Convention)

JavaScript does not have built-in protected properties like other languages.
However, the underscore _ naming convention is used to indicate that a property should be treated as protected.

Example: Protected Property Using _

class Person { constructor(name, age) { this.name = name; this._age = age; // Conventionally "protected" } getAge() { return this._age; } } class Employee extends Person { constructor(name, age, salary) { super(name, age); this.salary = salary; } showDetails() { console.log(`${this.name} is ${this._age} years old and earns $${this.salary}`); } } const emp = new Employee("John", 30, 5000); console.log(emp.name); // ✅ Output: John console.log(emp.getAge()); // ✅ Output: 30 console.log(emp._age); // ⚠️ Technically accessible, but should be avoided

_age is not truly protected, but developers should treat it as "internal use only."
Unlike private properties (#), _age can still be accessed and modified outside the class.

šŸ”¹ Private vs. Protected: Key Differences

FeaturePrivate (#)Protected (_ Convention)
Access from class✅ Allowed✅ Allowed
Access from subclass❌ Not Allowed✅ Allowed
Access from outside❌ Not Allowed⚠️ Technically possible but discouraged
Real encapsulation?✅ Yes, enforced by JavaScript❌ No, just a naming convention

šŸŽÆ Summary

Private properties/methods (#): Fully hidden, accessible only inside the class.
Protected properties (_): Accessible inside subclasses but not truly protected.
✔ Use private (#) for strong encapsulation.
✔ Use protected (_) if subclass access is needed, but follow conventions.

šŸš€ Need more clarification? Let me know! 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close