Skip to content

Instantly share code, notes, and snippets.

@UniBreakfast
Created October 14, 2021 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UniBreakfast/d929a2d2bfd3cc0f62466bd0d48beade to your computer and use it in GitHub Desktop.
Save UniBreakfast/d929a2d2bfd3cc0f62466bd0d48beade to your computer and use it in GitHub Desktop.
Getters and setter on rectangle and circle objects
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