JavaScript - 문제 미리보기
문제 629
medium
다음 코드의 실행 결과는?
```javascript
function Person(first, last) {
this.firstName = first;
this.lastName = last;
}
Person.nationality = "English";
const person1 = new Person("John", "Doe");
console.log(person1.nationality);
```
```javascript
function Person(first, last) {
this.firstName = first;
this.lastName = last;
}
Person.nationality = "English";
const person1 = new Person("John", "Doe");
console.log(person1.nationality);
```
정답: B
`Person.nationality = "English"`는 Person 생성자 함수 자체에 속성을 추가하는 것이므로, 생성된 객체 `person1`에서는 이 속성에 접근할 수 없습니다. 따라서 `person1.nationality`는 `undefined`를 반환합니다. 생성된 모든 객체가 접근할 수 있게 하려면 `Person.prototype.nationality = "English"`를 사용해야 합니다.
💡 학습 팁
이 문제를 포함한 JavaScript 과목의 모든 문제를 순차적으로 풀어보세요. 진행상황이 자동으로 저장되어 언제든지 이어서 학습할 수 있습니다.