점 표기법(객체명.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' }
1.키를 이용해서 할당된 value에 접근
책장 내에서 파일의 이름을 선택해서 파일 안에 작성된 데이터를 꺼낸다
대괄호[]를 이용해서 property를 조회하고 싶을 때
객체 참조변수['key']
대괄호는 언제든지 사용 가능
key의 값이 유효한 변수가 아닐 때 사용.
객체를 선언하고 key와 value를 이용해 property를 지정할 때
key의 이름에 공백이나 특수 문자가 있으면 유효하지 않다.
공백이 있을 때 따옴표 ''를 사용해 유효한 변수로 변경
person.bloodType = 'AB+';
delete person.gender;
person['bloodType'] = 'AB+';
delete person['gender'];