getters and setters

차노·2023년 8월 3일
0

JS

목록 보기
10/96

There are two kinds of object properties.

The first kind is data properties. The second type of property is something new. It's an accessor property. They are essentially function that execute on getting and setting a value.

The getter works when ojb.propName is read, the setter - when it is assigned.

For instance, we have a user object with name and surname:

get fulName() 이라는 함수를 만들고 this 객체를 이용하여 name 값과 surname 값을 가져온다. 그리고 alert 하수를 이용하여 user의 fullName을 호출한다.

Getter runs behind the scenes.

set 함수에서 split 메소드를 이용하여 name과 surname을 분리하고 그 값에 새로운 이름을 넣어 변경한다.

  • get - a function without arguments, that works when a property is read.
  • set - a function with one argument, that is called when the property is set.

Reference

0개의 댓글