Conversation

Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

Private Class Features in JavaScript

Private Class Features

When a method is classified as Private, it means that only those objects belonging to the same class can detect it. By default, class fields are public. To declare a private class field, prefix the name of the class field with # (hash) tag. The # is a part of the name, Private fields can be accessed on the Class Constructor from within the Class declaration. They are used for the declaration of field names as well as for accessing a field's value.

The procedure to create a private class field.

class MyClass {
  // Declare private class field
  #myPrivateField = "This is a personal account.";
}


Accessing Private Class Fields/Properties with Methods

There are two ways to access a class property:

  • Create a new instance of the class and access the properties of that instance.
  • Declare the property as a static property so that the class cannot be instantiated.

To access private class fields from outside the class, one can create a new method and return the private class field from that method. The method can be defined as public or static.

If the method is public, the class must be instantiated. Then, call that method on the new instance and get the value of the private field. Static methods can be called without instantiating the class.

The creation of a public method to access a private class field.

class MyClass {
  // Declare private class field
  #myPrivateField = "This is a personal account.";

  // Public method to access the private field
  getPrivateField() {
    return this.#myPrivateField;
  }
}

// Create an instance of MyClass
const myInstance = new MyClass();

// Access the private field via the public method
console.log(myInstance.getPrivateField()); // Output: This is a personal account.


Updating Private Class Fields with Methods

The same rules are applicable when updating private class fields. Use a method call from the outside and gain access to a private class field.

The procedure to update a private class field.

class MyClass {
  // Declare private class field
  #myPrivateField = "This is a personal account.";

  // Public method to access the private field
  getPrivateField() {
    return this.#myPrivateField;
  }

  // Public method to update the private field
  updatePrivateField(newValue) {
    this.#myPrivateField = newValue;
  }
}

// Create an instance of MyClass
const myInstance = new MyClass();

// Access the private field via the public method
console.log(myInstance.getPrivateField()); // Output: This is a personal account.

// Update the private field using the public method
myInstance.updatePrivateField("This is a new personal account.");

// Access the updated private field
console.log(myInstance.getPrivateField()); // Output: This is a new personal account.


JavaScript Private Class Private Class Features Private Class in JavaScript Private Class Field Private Class Fields with Methods Private Class Features in JavaScript

advertisement