getter and setter in class in javascript

What is getters in a class? What is setters in a class? Example of setters in a class Example of getters in a class syntax

getter and setter in class in javascript

Photo by Katie Harp on Unsplash

What is getters in a class?

Getters are special type of functions that is used to get/retrieve the value of an object property. It permits to define a method for access a specified object property.

Getters are used to calculating or formatting a property value. It should be called multiple times and should not modify the object they are defined on.

syntax

get propertyname( ) {

// code

}

Example of getters in a class

Here is a example of the setter and getter in a class setandget with private property #msg.

What is setters in a class?

It is a special type of function to set property of an object. It allows to define a method to be executed when a specified property is assigned a new value.

syntax

set propertyname (value){

//expression

}

Example of setters in a class

Here is a example of the setter and getter in a class setandget with private property #msg.