const person1 = { name: 'Alice', age: 25 };
const person2 = { name: 'Bob', age: 27 };
const spec1 = { job: 'developer' };
const spec2 = { degree: 'doctor' };
const spec3 = { cert: 'photoshop' };
// Object의 정적 메소드를 사용해서
// person1에는 spec1의 프로퍼티를,
// person2에는 spec1, spec2, spec3 모두의 프로퍼티들을 더해보세요.
// person1에 프로퍼티를 추가하거나 삭제하지 못하게 만들어보세요.
// person2를 읽기만 가능한 객체로 만들어보세요.
const privates = ['height', 'weight']
const person1 = {
name: '홍길동',
sex: 'M',
height: 175,
weight: 70,
family: ['부', '모', '누나'],
profession: {
job: 'developer',
position: '사원',
department: '프론트엔드'
}
}
// 위의 person1 객체를 privates에 속한 프로퍼티를 제외하고
// 깊은 복사를 하는 코드를 작성해보세요.
// 예시
const person1 = { name: 'Alice', age: 25 };
const person2 = { name: 'Bob', age: 27 };
const spec1 = { job: 'developer' };
const spec2 = { degree: 'doctor' };
const spec3 = { cert: 'photoshop' };
Object.assign(person1, spec1);
Object.assign(person2, spec1, spec2, spec3);
Object.seal(person1);
Object.freeze(person2);
// 예시
const privates = ['height', 'weight']
const person1 = {
name: '홍길동',
sex: 'M',
height: 175,
weight: 70,
family: ['부', '모', '누나'],
profession: {
job: 'developer',
position: '사원',
department: '프론트엔드'
}
}
const copied = JSON.parse(
JSON.stringify(person1),
(key, value) => {
if (privates.includes(key)) return undefined;
return value;
}
);
JSON으로 직렬화 및 파싱하는 과정에서 데이터의 요소들은 깊은 복사와 같은 방식으로 복제됩니다.
단 함수 및 특정 자료형들은 직렬화되지 않음을 기억하세요.