[javascript] 프로퍼티 디스크립터 사용 시 다중 프로퍼티 설정 예제

프로퍼티 디스크립터(property descriptor)는 JavaScript 객체의 속성(property)을 정의하는 데 사용되는 객체입니다. 이 객체를 사용하여 다양한 속성을 설정하거나 변경할 수 있습니다. 이번 예제에서는 다중 프로퍼티를 설정하는 방법을 알아보겠습니다.

const obj = {};

Object.defineProperties(obj, {
  name: {
    value: 'John',
    writable: true,
    enumerable: true,
    configurable: true
  },
  age: {
    value: 25,
    writable: false,
    enumerable: true,
    configurable: true
  },
  email: {
    value: 'john@example.com',
    writable: true,
    enumerable: true,
    configurable: false
  }
});

console.log(obj);

위 예제에서는 Object.defineProperties() 메서드를 사용하여 obj 객체의 다중 프로퍼티를 설정하고 있습니다. name, age, email이라는 세 개의 프로퍼티를 설정하고 각각의 값과 속성을 정의하였습니다.

위 코드를 실행하면 obj 객체의 프로퍼티들이 정의된 상태로 출력됩니다.

참고 자료: