728x90 반응형 SMALL 자바스크립트 롬복1 [javascript]자바스크립트에서 getter/setter 구현하기 + 접근제한자 private 이용하기 자바스크립트에서 getter / setter를 구현해보겠습니다. 1. 접근제한자 X class Person { constructor(name) { this._name = name; } get name() { return this._name; } set name(name) { this._name = name; } } const person = new Person('John'); console.log(person.name); // Output: 'John' person.name = 'Jane'; console.log(person.name); // Output: 'Jane' 그러나 접근제한자를 설정을안해주어서 _name의 변수는 언제든 바뀔수 있다. 또는 getName을 통하지않고도 마구마구 쓰일 수 있다. .. 2023. 1. 5. 이전 1 다음 728x90 반응형 LIST