Answers for "setters code greeper"

0

setters code greeper

// this is an example of setters and getters of javascript classes 
class Square {
    constructor(width){
        this.width = width ;
        this.height = width ;
        this.numOfRequests = 0;
    }

   get area(){
       this.numOfRequests ++ ;
        return this.width * this.height ;
    }

    set area(area){
        this.width = Math.sqrt(area);
        this.height = Math.sqrt(area);
    }
}

const newSquare = new Square(6);
console.log(newSquare.area);
console.log(newSquare.area);
console.log(newSquare.area);
console.log(newSquare.area);
console.log(newSquare.area);
console.log(newSquare.area);
console.log(newSquare.area);

console.log(newSquare.numOfRequests);
Posted by: Guest on August-08-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language