-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path037-Getter-Setter.js
42 lines (30 loc) · 1.03 KB
/
037-Getter-Setter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
author : Jaydatt Patel;
Getter: The get syntax binds an object property to a function that will be called when that property is looked up. It can also be used in classes.
Note: You can not pass parameters in get method.
syntax:
get Method_name(){
}
Setter: The set syntax binds an object property to a function to be called when there is an attempt to set that property. It can also be used in classes.
Note: You can pass only one parameters in get method.
syntax:
set Method_name(obj){
}
*/
class Circle {
constructor(radius) {
this.radius = radius;
}
get diameter() {
return this.radius * 2;
}
set diameter(newDiameter) {
this.radius = newDiameter;
}
}
const circle = new Circle(4);
console.log("circle.radius : ", circle.radius);
console.log("circle.diameter : ", circle.diameter);
circle.diameter = 20;
console.log("circle.radius : ", circle.radius);
console.log("circle.diameter : ", circle.diameter);