const Image = {
length: 8889,
meta: {
name: 'foo',
type: '.jpg',
device: {
type: 'mobile',
company: 'sam',
},
},
};
위와 같은 객체가 있을 때 객체가 중첩된 경우
어떻게 깊이 복사 할 것인가
라는 문제에 대해서
immer / lodash 같은 라이브러리를 쓰거나 아니면 재귀로 순회해서 객체를 복사하는
방법이 있다고 생각했는데
JSON.stringify를 사용하면 훨씬 간단하고 성능적인 이점도 있다는걸 알게되었다
function deepFreeze(object) {
// 객체에 정의된 속성명을 추출
var propNames = Object.getOwnPropertyNames(object);
// 스스로를 동결하기 전에 속성을 동결
for (let name of propNames) {
let value = object[name];
object[name] = value && typeof value === "object" ? deepFreeze(value) : value;
}
return Object.freeze(object);
}
이건 재귀적으로 탐색하는 경우에 응용할수 있는 코드