dot notation
(객체이름).(key이름)
bracket notation
객체이름[key이름]
<aside> 💡 변수를 활용해서 객체에 접근할 수 있다.
</aside>
두 접근법의 차이?
Dot Notation | Bracket Notation | |
---|---|---|
key에~ | ||
숫자가 포함된 경우 | O | O |
숫자로 시작한 경우 | X | O |
숫자로만 이루어진 경우 | X | O |
특수 문자로 시작한 경우 | X | O |
특수 문자가 포함한 경우 | X | O |
특수 문자로만 이루어진 경우 | X | O |
$ 또는 _ 를 포함하거나 시작하는 경우 | O | O |
띄워쓰기 포함한 경우 | X | O |
변수 포함한 경우 | X | O |
동적인 접근 | O |
→ $ 또는 _ 포함하거나 시작할 경우 예시.
const obj = {
title: 'The Title',
name: 'Jane',
contents: 'Nothing to say',
$11: "one"
};
obj['$11']//'one'
obj.$11 //'one'
→ 변수 포함한 경우 예시
let myArray = {
city : ['busan', 'seoul']
}
let myCity = 'city'
/* Bracket Notation */
console.log(myArray['city']);
// ['busan', 'seoul']
console.log(myArray[myCity]);
// ['busan', 'seoul']
/* Dot Notation */
console.log(myArray.myCity);
// undefined