TypeScript Accessor
In TypeScript, the accessor property provides a method to access and set the class members. It has two methods which are given below.
- getter
- setter
getter
The getter accessor property is the conventional method which is used for retrieving the value of a variable. In object literal, the getter property denoted by “get” keyword. It can be public, private, and protected.
Syntax
Example
Setter
The setter accessor property is the conventional method which is used for updating the value of a variable. In object literal, the setter property is denoted by “set” keyword.
Syntax
Example
We can understand the concept of getter and setter from the below example.
Example
Output:
Now, if we change the first line: let passcode = “secret_passcode”;
Then, Output: Unauthorized update of student detail!
Naming convention for getter and setter
The naming convention of the setter and getter method should be as follows:
getXX() and setXX()
Here, XX is the name of the variable. For example:
Then the setter and getter will be: