assing
-
Modren JS - 객체와 변경불가성(Immutability)신나는 JavaScript 2023. 1. 15. 15:24
변경불가성(Immutability)는 객체가 생성된 이후 그 상태를 변경할 수 없는 디자인 패턴을 의미함. var user1 = { name: 'Lee', address: { city: 'Seoul' } }; var user2 = user1; // 변수 user2는 객체 타입이다. user2.name = 'Kim'; console.log(user1.name); // Kim console.log(user2.name); // Kim 위의 코드처럼 되는 이유는 user1과 user2가 같은 address를 참조하고 있긴 때문임 이러한 것이 의도된 것이 아니라면 주의가 필요 ! 불변 데이터 패턴(immutable data pattern) 객체의 방어적 복사(defensive copy) -Object.assign..