You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. This gives a way of having finer-grained control over how a member is accessed on each object.
126
+
TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. Getters enable us to bind a property to a function that is called when the property is accessed, whereas setters bind a property to a function that is called on attempts to set the property.
127
+
128
+
**Example:**
127
129
128
130
```ts
131
+
/**
132
+
* Getters and Setters
133
+
*/
129
134
classEmployee {
130
-
131
-
private _name:string;
132
-
133
-
get Name() {
134
-
returnthis._name;
135
-
}
136
-
set Name(val) {
137
-
this._name=val;
138
-
}
135
+
private _name:string;
136
+
137
+
get Name() {
138
+
returnthis._name;
139
+
}
140
+
set Name(val) {
141
+
this._name=val;
142
+
}
139
143
}
144
+
145
+
let employee =newEmployee();
146
+
employee.Name="Pradeep";
147
+
148
+
console.log("Name"+employee.Name); // Pradeep
140
149
```
141
150
151
+
**⚝[Try this example on CodeSandbox](https://codesandbox.io/s/getter-and-setter-peq1nj?file=/src/index.ts)**
0 commit comments