1️⃣ 객체 프로퍼티 접근 및 조회

  • 점 표기법(객체명.key)과 대괄호 표기법(객체명['key'])으로 프로퍼티에 접근할 수 있습니다.
  • 키에 공백 또는 특수문자가 포함된 경우 대괄호 표기법을 사용해야 합니다.
  • 객체의 프로퍼티 값을 조회하거나 변경할 수 있습니다.
const person = {
  name: 'John',
  age: 30,
  gender: 'male',
  'place of birth': 'Seoul',
};

console.log(person.name); // 점 표기법
console.log(person.age); // 점 표기법
console.log(person['gender']); // 대괄호 표기법
console.log(person['place of birth']); // 대괄호 표기법
// 결과
John
30
male
Seoul
person.age = 20;
console.log(person);
// 결과
{ name: 'John', age: 20, gender: 'male', 'place of birth': 'Seoul' }

 

2️⃣ 객체 프로퍼티 추가 및 삭제

점 표기법 사용하기

  • object.key = value 형식으로 객체에 새로운 프로퍼티를 추가하거나 수정할 수 있습니다.
  • delete object.key 형식으로 객체의 프로퍼티를 삭제할 수 있습니다.
person.bloodType = 'AB+';
delete person.gender;

 

대괄호 표기법 사용하기

  • object['key'] = value 형식으로 객체에 새로운 프로퍼티를 추가하거나 수정할 수 있습니다.
  • delete object['key'] 형식으로 객체의 프로퍼티를 삭제할 수 있습니다.
person['bloodType'] = 'AB+';
delete person['gender'];

 

 

객체에 생성된 데이터 key, value로 구성된 property 입니다.

 

 

'자바스크립트' 카테고리의 다른 글

배열이란?  (0) 2024.12.26
삼항연산자  (1) 2024.12.16
로또 번호 생성기 과제  (0) 2024.12.16
switch 함수  (0) 2024.12.12
if 문 / if ...else 문/ else if 절  (0) 2024.12.11

+ Recent posts