Public instance method getter

A public instance method getter is a special kind of method that allows you to access the value of a property of an object or a class instance. A getter is defined using the `get` keyword before the method name, and it does not take any parameters. A getter can be used to perform some computation or validation before returning the property value, or to create a read-only property.

Why use public instance method getters?

Public instance method getters are useful for the following reasons:

  • They provide a way to encapsulate the internal state of an object or a class instance, and expose only the necessary information to the outside world.
  • They allow you to define custom logic for accessing a property, such as checking for errors, caching results, or logging actions.
  • They make the code more readable and consistent, as you can use the dot notation (obj.prop) to access a property, instead of calling a method (obj.getProp()).

How to define public instance method getters?

You can define a public instance method getter using the following syntax:

Javascript
                        
class ClassName {
  // define a property
  prop = "some value";

  // define a getter for the property
  get prop() {
    // getter body
    return this.prop;
  }
}

You can access a public instance method getter using the dot notation (instance.prop), as if it was a regular property. The getter will be invoked automatically when you access the property, and it will return the value of the property.

Note: You cannot assign a value to a getter, as it is read-only. If you want to define a setter for the property, you need to use the set keyword and provide a parameter for the new value. For more details, see the article on public instance method setter.

Example of public instance method getter

Let's see an example of how to use a public instance method getter in a class. Suppose you have a class called Circle, which represents a geometric shape with a radius. You want to define a getter for the area of the circle, which is calculated by multiplying the square of the radius by the constant Math.PI.

Javascript
                        
class Circle {
  // define a constructor method
  constructor(radius) {
    // assign the radius to the instance
    this.radius = radius;
  }

  // define a getter for the area
  get area() {
    // calculate and return the area
    return Math.PI * this.radius ** 2;
  }
}

// create a new instance of the class
const circle = new Circle(10);

// access the getter for the area
console.log(circle.area); // 314.1592653589793

In this example, you can see that the getter for the area is defined using the get keyword and the name of the property. The getter does not take any parameters, but it uses the this keyword to access the radius of the instance. The getter returns the value of the area, which is computed using the formula Math.PI * this.radius ** 2. You can access the getter using the dot notation (circle.area), and it will return the value of the area. You cannot assign a value to the getter, as it is read-only.

Conclusion

In this article, you learned about public instance method getters in JavaScript, which are special methods that allow you to access the value of a property of an object or a class instance. You learned how to define a getter using the `get` keyword, and how to access it using the dot notation. You also learned the benefits of using getters, such as encapsulating the internal state, defining custom logic, and making the code more readable.