Public Static Method, Getter, Setter, and Field in JavaScript

In this article, we will learn about public static method, getter, setter, and field in JavaScript. Public static features are properties and methods that belong to the class itself, not to the instances of the class. They can be accessed and modified by anyone, using the class name as a prefix.

Why Use Public Static Features?

Public static features are useful for several reasons:

  • They can provide utility functions and constants that are related to the class, but do not depend on the instance state or behavior.
  • They can provide access to shared data or resources that are common to all instances of the class, such as caches, configurations, or counters.
  • They can provide factory methods or constructors that create and return instances of the class, or subclasses of the class, based on some parameters or conditions.

How to Define Public Static Features?

We can define public static features in JavaScript classes using the static keyword, followed by the property or method definition. The property or method name can be any valid identifier, or a computed expression enclosed in brackets. The property can have an optional initial value, or a getter and setter pair. The method can have an optional parameter list and a body.

The syntax of defining a public static feature is as follows:

Javascript
                      
class ClassName {
  // Define the public static property with an optional initial value
  static propertyName = initialValue;
  // Define the public static property with a computed name and an optional initial value
  static [computedName] = initialValue;
  // Define the public static getter and setter pair
  static get propertyName() {
    // Return the property value
  }
  static set propertyName(newValue) {
    // Validate and assign the new value to the property
  }
  // Define the public static method
  static methodName(parameterList) {
    // Perform the method logic
  }
}

Here is an example of defining public static features for a counter property and a create method in a User class:

Javascript
                      
class User {
  // Define the public static property counter with an initial value
  static counter = 0;
  // Define the public static method create with a parameter
  static create(name) {
    // Increment the counter
    User.counter++;
    // Return a new instance of the class with the name
    return new User(name);
  }
  // Define the constructor with a parameter
  constructor(name) {
    // Assign the name to the instance
    this.name = name;
  }
}

How to Use Public Static Features?

We can use public static features in JavaScript classes by accessing or assigning them using the dot notation or the bracket notation, just like any other property or method. However, we need to use the class name as a prefix, not the instance name. For example, if we have a user instance of the User class, we can use the counter and the create features as follows:

Javascript
                      
// Access the counter property using the dot notation
console.log(User.counter); // 0
// Call the create method using the bracket notation
const user = User["create"]("Alice");
// Access the name property of the instance using the dot notation
console.log(user.name); // "Alice"
// Access the counter property again using the dot notation
console.log(User.counter); // 1

Both statements will access or call the public static features of the User class, not the user instance. They will also affect all the instances of the class, as they share the same public static features.

Conclusion

In this article, we have learned about public static method, getter, setter, and field in JavaScript. Public static features are properties and methods that belong to the class itself, not to the instances of the class. They can be accessed and modified by anyone, using the class name as a prefix. Public static features can provide utility functions, constants, shared data, and factory methods that are related to the class. Public static features can make our code more readable and consistent, and enhance the functionality of our classes..