getter & setter

cos·2022년 8월 27일
0

getter(획득자) 메서드는 프로퍼티를 읽으려고 할 때 실행된다.
setter(설정자) 메서드는 프로퍼티에 값을 할당하려 할 때 실행된다.

const person = {
  _firstName: 'junyoung',
  lastName: 'kim',

  get firstName() {
    return this._firstName.toUpperCase();
  },

  set firstName(newFirstName) {
    if (typeof newFirstName !== 'string') {
      this._firstName = 'undefined name';
    }
    return;
  },
};

person.firstName = 123456;
console.log(person.firstName);
profile
The journey is the reward

0개의 댓글