갈푸라떼
갈푸라떼는 개발중
갈푸라떼
전체 방문자
오늘
어제
  • 분류 전체보기 (232)
    • CS (0)
      • CSinfo (0)
    • Frontend (15)
      • HTML,CSS (1)
      • Javascript (2)
      • React (0)
      • React Hook (12)
    • Backend (0)
      • Python (0)
      • Node.js (0)
      • php (0)
    • DB (2)
      • MySQL (2)
      • BigQuery (0)
      • Mongodb (0)
    • Study (186)
      • JavaScript (72)
      • JavaScript(Clean code) (50)
      • Node.js (11)
      • HTML,CSS (13)
      • React (30)
      • TypeScript (10)
      • React-Native (0)
    • Error (2)
      • error (2)
    • Git (22)
      • Git (22)
    • Help Coding (4)
      • Useful websites (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • Github

공지사항

인기 글

태그

  • 객체타입
  • PM2
  • 프로토타입
  • symbol
  • 호이스팅
  • 네이밍
  • 정적 레벨
  • 렉시컬 환경
  • 컴파일러
  • 자바스크립트엔진
  • 오버라이딩
  • this
  • 스코프 체인
  • 심볼
  • 원시타입
  • Arrow
  • 싱글스레드
  • ECMAScript
  • 상속
  • class
  • 이터러블
  • SPREAD
  • 실행 컨텍스트
  • 함수
  • nodemon
  • function
  • 인터프리터
  • prototype
  • Babel
  • structure

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
갈푸라떼

갈푸라떼는 개발중

Study/JavaScript

[JavaScript_study] 프로퍼티(property)

2022. 4. 14. 03:33

* 프로퍼티(property)

프로퍼티는 프로퍼티 키(이름)와 프로퍼티 값으로 구성된다. 프로퍼티는 프로퍼티 키로 유일하게 식별할 수 있다. 즉, 프로퍼티 키는 프로퍼티를 식별하기 위한 식별자(identifier)다. 프로퍼티 키의 명명 규칙과 프로퍼티 값으로 사용할 수 있는 값은 아래와 같다.

 

  • 프로퍼티 키 : 빈 문자열을 포함하는 모든 문자열 또는 symbol 값
  • 프로퍼티 값 : 모든 값
const dog = { name: '와우', emoji: '강아지' };

console.log(Object.keys(dog));
console.log(Object.values(dog));
// [key, value] 쌍의 배열을 반환
console.log(Object.entries(dog));

// 특정 오브젝트에 해당 key가 있는지 없는지 체크하는 in연산자
console.log('name' in dog);
// 특정 오브젝트에 해당 key가 있는지 없는지 체크하는 hasOwnProperty
console.log(dog.hasOwnProperty('name'));

// 오브젝트의 각각의 프로퍼티는 프로퍼티 디스크립터라고 하는 객체로 저장됨
const descriptors = Object.getOwnPropertyDescriptors(dog);
console.log(descriptors);

// 특정한 key에 해당하는 디스크립터만 가져올 경우
const desc = Object.getOwnPropertyDescriptor(dog, 'name');
console.log(desc);

// defineProperty를 이용해서 Object안에 있는 PropertyDescriptor를 수정이 가능하다.
Object.defineProperty(dog, 'name', {
  value: '멍멍',
  writable: false,
  enumerable: false,
  configurable: false,
});

console.log(dog.name);
// enumerable이 false이기 때문에 ['emoji']만 출력된다.
console.log(Object.keys(dog));
// configurable이 false이기 때문에 값을 삭제할 수 없다.
delete dog.name;
console.log(dog.name);

const student = {};
Object.defineProperties(student, {
  firstName: {
    value: '영희',
    writable: true,
    enumerable: true,
    configurable: true,
  },
  lastName: {
    value: '김',
    writable: true,
    enumerable: true,
    configurable: true,
  },
  fullName: {
    get() {
      return `${lastName} ${firstName}`;
    },
    set(name) {
      [this.lastName, this.firstName] = name.split(' ');
    },
    configurable: true,
  },
});
console.log(student);

'Study > JavaScript' 카테고리의 다른 글

[JavaScript_study] 상속(inheritance) // prototype, class  (0) 2022.04.14
[JavaScript_study] freeze (불변성을 추구할 때)  (0) 2022.04.14
[JavaScript_study] 프로토타입(Prototype)  (0) 2022.04.14
[JavaScript_study] 엄격 모드 (strict mode)  (0) 2022.04.14
[JavaScript_study] var을 쓰지 말자  (0) 2022.04.14
    'Study/JavaScript' 카테고리의 다른 글
    • [JavaScript_study] 상속(inheritance) // prototype, class
    • [JavaScript_study] freeze (불변성을 추구할 때)
    • [JavaScript_study] 프로토타입(Prototype)
    • [JavaScript_study] 엄격 모드 (strict mode)
    갈푸라떼
    갈푸라떼

    티스토리툴바