2020.06.28 object

d·2020년 6월 27일
0
post-thumbnail

let nestedObj = {
type: {
year: '2019',
'comment-type': [{
name: 'simple'
}]
}
}

위에서 simple을 출력하려면,
console.log(nestedOnj.type['comment-type'][0].name);

밑에 따라서 2번 반복해서 입력해보았다.
console.log(nesetedObg.type['comment-type'][0].name);

console.log(nestedObj.type['comment-type'][0].name);

nestedObj.type는 year.comment-type이 있는 객체입니다.
nestObj.type['comment-type']는 요소 1개인 배열입니다.
nestObj.type['comment-type'][0] 첫번째 요소인 객체를 가져옵니다.
nestObj.type['comment-type'][0].name 드디어 'simple'에 접근!

####객체는 reference로 저장된다.
객체는 refrence가 저장됩니다.
객체를 변수에 저장하면 객체 리터럴 자체가 저장되는 것이 아니라,
reference가 저장됩니다.

텍스트는 변수에 저장하면 텍스트 자체가 저장됩니다.
그래서 같은 텍스트면 서로 값이 같으므로 true가 나옵니다.

const a = '안녕';
const b = '안녕';
console.log(a === b);

그런데, 아래의 객체는 생긴모양이 아예 똑같은데
false라 출력됩니다.

const hiObj = {
name: '안녕'
};

const helloObj = {
name: '안녕'
};

console.log('객체비교 =>'. hiObj === helloObj);
그 이유는 객체는 변수에 저장할 때, 객체 자체를 그대로 저장하는 것이 아니기 때문입니다.
객체가 담긴 어느 메모리의 reference를 저장하기 때문입니다..

참고로, 객체는 {}만 사용하면 되고, 이렇게 {}으로 생긴 모양의 객체를 object literal(객체리터럴)이라 부릅니다.

profile
d

0개의 댓글