원시 값과 객체타입(참조타입)의 값을 비교해보자
참조에 의한 전달
)- 두 개의 식별자가 하나의 객체를 공유 : 객체를 가리키는 변수(원본, person)를 다른 변수(사본, copy)에 할당하면 원본의 메모리 주소 값이 복사되어 전달된다. person과 copy는 다른 메모리 주소를 가지고 있지만, 동일한 참조 값을 갖고 있다. 따라서 원본이나 사본 중 어느 한 쪽에서 객체를 변경하면(객체의 프로퍼티 값을 변경하거나 추가, 삭제하면) 서로 영향을 주고받는다.
let person = {
name: 'Lee'
};
let copy = person;
console.log(copy) ; // person과 동일한 출력결과
copy.name = 'Kim';
person.address = 'Seoul';
console.log(person); // {name: 'Kim', address: 'Seoul'}
console.log(copy); // {name: 'Kim', address: 'Seoul'}