1. getter, setter 활용 const myObj = { this.someProperty = someValue; get someProperty() { return this.someProperty } set someProperty(newValue) { this.someProperty = newValue; alert("someProperty is changed!"); } }; 2. defineProperty 활용 defineProperty 메소드는 세 번째 파라미터로 descriptor를 제공하는데, 여기에서도 getter, setter를 설정할 수 있다. const myObj = { someProperty: someValue, }; Object.defineProperty(myObj, "somePr..
JavaScript 에서도 getter, setter 를 지원한다. 사용하는 방법에 대한 설명은 어디에서나 찾을 수 있었는데, 언제 그리고 왜 사용해야하는지는 찾을 수 없어서 직접 케이스를 만들어 보았다. Use case: 자료구조형 클래스 선언 // 기본 클래스 선언 class Bounds { constructor(southWest, northEast) { this.south = southWest.south; this.west = southWest.west; this.north = northEast.north; this.east = northEast.east; } } southWest, northEast 값을 한 번에 얻고 싶다는 요구 사항이 들어온다면? class Bounds { constructor..
- Total