Public Class Fields in JavaScript

In this article, we will learn about public class fields in JavaScript. Public class fields are properties that belong to each instance of a class, and can be accessed and modified by anyone. They are similar to public instance setters, but they do not require a method definition or a parameter.

Why Use Public Class Fields?

Public class fields are useful for several reasons:

  • They can provide a clear and concise way to declare and initialize instance properties, without using the constructor or assignment statements.
  • They can make the class definition more self-documenting, by showing the available properties and their default values.
  • They can avoid the common pitfall of forgetting to use `this` when accessing or assigning instance properties, as they are automatically bound to the instance.

How to Define Public Class Fields?

We can define public class fields in JavaScript classes using the `=` operator, 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 public class field is as follows:

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

Here is an example of defining public class fields for a `name` and an `age` property in a `Person` class:

Javascript
                      
class Person {
  // Define the name property with an initial value
  name = "Anonymous";
  // Define the age property with a computed name and an initial value
  [Math.random() > 0.5 ? "age" : "years"] = 0;
}

How to Use Public Class Fields?

We can use public class fields in JavaScript classes by accessing or assigning them using the dot notation or the bracket notation, just like any other property. For example, if we have a `person` instance of the `Person` class, we can use the `name` and the `age` fields as follows:

Javascript
                      
// Access the name field using the dot notation
console.log(person.name); // "Anonymous"
// Assign a new value to the name field using the bracket notation
person["name"] = "Alice";
// Access the age field using the bracket notation
console.log(person.age); // 0
// Assign a new value to the age field using the dot notation
person.age = 25;

Conclusion

In this article, we have learned about public class fields in JavaScript. Public class fields are properties that belong to each instance of a class, and can be accessed and modified by anyone. They can provide a clear and concise way to declare and initialize instance properties, without using the constructor or assignment statements. They can also make the class definition more self-documenting, and avoid the common pitfall of forgetting to use `this`. Public class fields can make our code more readable and consistent, and enhance the functionality of our classes.