Property Descriptor

Denz
3 min readNov 29, 2015

--

ES5 이후로 모든 property는 data property 거나 accessor property 이다. data property는 writable 속성을 가진 value 를 가지며 accessor property는 getter-setter 함수를 가진다.

data property는 value, writable, enumerable, configurable attribute를 가진다. accessor property는 set, get, enumerable, configurable attribute를 가진다.
( enumerable과 configurable은 공통 )

descriptor는 property의 attribute를 설명하는 객체이다.( attribute값을 가진 )

data property의 descriptor.

  • Value: property 값. 기본값은 undefined.
  • Writable: 할당 연산자( = )를 통해 변경 가능한지 여부를 나타냄. 기본값은 false.
  • Configurable: true 면 property의 attribute를 수정할 수 있고 property를 삭제할 수도 있다. 기본값은 false.
  • Enumerable: for…in 이나 Object.keys() 메서드에 노출 여부. 기본 값은 false.
configurable = false; 인 경우 삭제 할 수 없음.

accessor property의 descriptor.

  • Get: property 값을 반환하는 함수. 함수의 파라미터는 없고 기본값은 undefined.
  • Set: property 값을 설정하는 함수.
  • Configurable: true 면 property의 attribute를 수정할 수 있고 property를 삭제할 수도 있다. 기본값은 false.
  • Enumerable: for…in 이나 Object.keys() 메서드에 노출 여부. 기본 값은 false.

descriptor 객체의 properties에 따라 data property인 지 accessor property 인지를 자동으로 결정한다.

만약 Reflect.defineProperty(), Object.defineProperty(), Object.defineProperties(), Object.create() 등의 메서드를 사용하지 않고 property를 추가했다면 property는 data property가 되고 writable, enumerable, configurable attribute는 모두 true로 설정된다.

property 추가 후에도 attribute는 수정 할 수 있다.

--

--