JavaScript - 문제 미리보기
문제 630
medium
다음 코드의 빈칸에 들어갈 올바른 코드는?
```javascript
function Person(first, last) {
this.firstName = first;
this.lastName = last;
}
Person.________.name = function() {
return this.firstName + " " + this.lastName;
};
const person1 = new Person("John", "Doe");
console.log(person1.name()); // "John Doe"
```
```javascript
function Person(first, last) {
this.firstName = first;
this.lastName = last;
}
Person.________.name = function() {
return this.firstName + " " + this.lastName;
};
const person1 = new Person("John", "Doe");
console.log(person1.name()); // "John Doe"
```
정답: C
생성자 함수에 새로운 메서드를 추가하려면 `prototype` 속성을 사용해야 합니다. `Person.prototype.name = function() { ... }`은 모든 Person 객체가 `name()` 메서드를 상속받을 수 있게 해줍니다. 이렇게 하면 `person1.name()`을 호출할 때 "John Doe"가 반환됩니다.
💡 학습 팁
이 문제를 포함한 JavaScript 과목의 모든 문제를 순차적으로 풀어보세요. 진행상황이 자동으로 저장되어 언제든지 이어서 학습할 수 있습니다.