Private Properties in JavaScript

In this article, we will learn about private properties in JavaScript. Private properties are properties that belong to each instance of a class, but cannot be accessed or modified by anyone outside of the class. They are similar to public properties, but they have a hash # prefix and are enforced by JavaScript itself.

Why Use Private Properties?

Private properties are useful for several reasons:

  • They can provide encapsulation and abstraction for the class, hiding the implementation details and exposing only the public interface.
  • They can prevent accidental or malicious manipulation of the property values, ensuring the integrity and consistency of the class behavior.
  • They can avoid name conflicts and collisions with other properties or methods, especially when inheriting from other classes or using mixins.

How to Define Private Properties?

We can define private properties in JavaScript classes using the # prefix, followed by the property name and an optional initial value. The property name can be any valid identifier, or a computed expression enclosed in brackets. The initial value can be any valid expression, or omitted to default to undefined.

The syntax of defining a private property is as follows:

Javascript
                      
class ClassName {
  // Define the private property with an optional initial value
  #propertyName = initialValue;
  // Define the private property with a computed name and an optional initial value
  #[computedName] = initialValue;
}

Here is an example of defining private properties for a name and an age property in a Person class:

Javascript
                      
class Person {
  // Define the private name property with an initial value
  #name = "Anonymous";
  // Define the private age property with a computed name and an initial value
  #[Math.random() > 0.5 ? "age" : "years"] = 0;
  // Define a public method to access the private name property
  getName() {
    return this.#name;
  }
}

How to Use Private Properties?

We can use private properties in JavaScript classes by accessing or assigning them using the dot notation or the bracket notation, just like any other property. However, we can only do so within the class body, not outside of it. For example, if we have a person instance of the Person class, we can use the name and the age properties as follows:

Javascript
                      
// Access the private name property using the dot notation
console.log(person.#name); // Syntax error
// Assign a new value to the private name property using the bracket notation
person["#name"] = "Alice"; // Syntax error
// Access the private age property using the bracket notation
console.log(person.age); // Syntax error
// Assign a new value to the private age property using the dot notation
person.age = 25; // Syntax error
// Access the private name property using the public
console.log(person.getName()); // "Anonymous"

All statements except the last one will throw a syntax error, as they are trying to access or assign private properties outside of the class. The last statement will work, as it is using a public method that is defined within the class and has access to the private properties.

Conclusion

In this article, we have learned about private properties in JavaScript. Private properties are properties that belong to each instance of a class, but cannot be accessed or modified by anyone outside of the class. They have a hash # prefix and are enforced by JavaScript itself. Private properties can provide encapsulation, abstraction, integrity, and consistency for the class. Private properties can make our code more secure and robust, and enhance the functionality of our classes.