Created
October 14, 2021 16:47
-
-
Save UniBreakfast/d929a2d2bfd3cc0f62466bd0d48beade to your computer and use it in GitHub Desktop.
Getters and setter on rectangle and circle objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rectangle = { | |
a: 4, | |
b: 5, | |
get perimeter() { | |
return this.a * 2 + this.b * 2; | |
}, | |
get area() { | |
return this.a * this.b; | |
}, | |
}; | |
const circle = { | |
radius: 5, | |
get diameter() { | |
return this.radius * 2; | |
}, | |
set diameter(num) { | |
this.radius = num / 2; | |
}, | |
get perimeter() { | |
return 2 * Math.PI * this.radius; | |
}, | |
set perimeter(num) { | |
this.radius = num / Math.PI / 2; | |
}, | |
get area() { | |
return Math.PI * this.radius**2; | |
}, | |
set area(num) { | |
this.radius = Math.sqrt(num / Math.PI) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment