JavaScript - 문제 미리보기
문제 625
medium
다음 코드의 실행 결과는?
```javascript
const person1 = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person2 = {
firstName: "John",
lastName: "Doe"
}
const result = person1.fullName.call(person2);
console.log(result);
```
```javascript
const person1 = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person2 = {
firstName: "John",
lastName: "Doe"
}
const result = person1.fullName.call(person2);
console.log(result);
```
정답: B
`call()` 메서드는 함수를 호출하면서 `this`가 참조할 객체를 첫 번째 인수로 지정할 수 있습니다. `person1.fullName.call(person2)`는 `person1`의 `fullName` 메서드를 `person2`를 `this`로 사용하여 호출합니다. 따라서 `this.firstName`은 "John", `this.lastName`은 "Doe"가 되어 "John Doe"가 출력됩니다.
💡 학습 팁
이 문제를 포함한 JavaScript 과목의 모든 문제를 순차적으로 풀어보세요. 진행상황이 자동으로 저장되어 언제든지 이어서 학습할 수 있습니다.