클래스 (Class) 2
javascript
의 class
의 getter
와 setter
도 다른 언어의 사용의도와 크게 다르지 않으며
선언하는 방식 역시 비슷하다고 볼 수 있다.
하지만 각 언에 따라서 getter
와 setter
선언 방식이 약간씩 다르다.
class Square { constructor (_width) { this.width = _width; this.height = _width; this.numOfRequestForArea = 0; } // getter get area () { this.numOfRequestForArea++; return this.width * this.height; } // setter set area (area) { // root this.width = Math.sqrt(area); this.height = this.width; } } let square = new Square(4); square.area = 25; console.log(square.area); console.log(square.area); console.log(square.area); console.log(square.area); console.log(square.numOfRequestForArea);

'JavaScript > Vanilla JS' 카테고리의 다른 글
정렬하기 array.sort() (0) | 2021.03.02 |
---|---|
클래스 (Class) 3 (0) | 2021.02.24 |
클래스 (Class) (0) | 2021.02.22 |
로컬스토리지 (localStorage) (0) | 2021.02.18 |
날짜 객체 (new Date()) (0) | 2021.02.09 |
댓글
이 글 공유하기
다른 글
-
정렬하기 array.sort()
정렬하기 array.sort()
2021.03.02 -
클래스 (Class) 3
클래스 (Class) 3
2021.02.24 -
클래스 (Class)
클래스 (Class)
2021.02.22 -
로컬스토리지 (localStorage)
로컬스토리지 (localStorage)
2021.02.18
댓글을 사용할 수 없습니다.