
Object.assign(target, source) 함수는 target 객체에 source 객체의 속성을 복사하는 방법입니다.
이 함수는 target 객체에 source 객체의 속성을 복사합니다.
만약 target 객체에 이미 동일한 속성이 존재할 때, source 객체의 속성이 target 객체의 속성을 덮어씁니다.
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
javascript
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };
Object.assign(target, source);
console.log(target); // { a: 1, b: 3, c: 4 }
위 코드에서, target 객체에 source 객체의 속성이 복사되며, target 객체의 b 속성이 source 객체의 b 속성을 덮어씁니다.
만약 target 객체에 source 객체의 속성이 존재하지 않을 때, source 객체의 속성이 target 객체에 추가됩니다.
#hostingforum.kr
javascript
const target = { a: 1, b: 2 };
const source = { c: 3, d: 4 };
Object.assign(target, source);
console.log(target); // { a: 1, b: 2, c: 3, d: 4 }
위 코드에서, target 객체에 source 객체의 속성이 추가됩니다.
Object.assign 함수는 target 객체에 source 객체의 속성을 복사하는 방법이며, target 객체에 이미 동일한 속성이 존재할 때, source 객체의 속성이 target 객체의 속성을 덮어씁니다.
2025-03-30 17:52