
1. Object.assign(target, source) 사용법에 대해 알려주세요.
Object.assign(target, source)은 객체의 속성을 복사하는 메서드입니다. target 객체에 source 객체의 속성을 복사합니다. 예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
javascript
let target = { a: 1, b: 2 };
let source = { b: 3, c: 4 };
Object.assign(target, source);
console.log(target); // { a: 1, b: 3, c: 4 }
2. Object.assign(target, source)에서 target과 source가 무엇인지 설명해주세요.
target은 복사할 대상 객체입니다. source는 복사할 객체입니다. target 객체의 속성이 source 객체의 속성을 덮어씁니다.
3. Object.assign(target, source)에서 source의 속성이 target에 복사되는 방식에 대해 알려주세요.
Object.assign(target, source)에서 source의 속성이 target에 복사되는 방식은 다음과 같습니다.
- target 객체의 속성이 source 객체의 속성을 덮어씁니다.
- source 객체의 속성이 target 객체에 추가됩니다.
- null 또는 undefined 값을 가진 속성은 target 객체에 추가되지 않습니다.
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
javascript
let target = { a: 1, b: 2 };
let source = { b: 3, c: 4, d: null };
Object.assign(target, source);
console.log(target); // { a: 1, b: 3, c: 4 }
2025-04-16 13:27