인프런 커뮤니티 질문&답변
퀴즈
47%나 틀려요. 한번 도전해보세요!
JavaScript에서 new 연산자가 생성자 함수와 함께 사용될 때, 주요 역할은 무엇일까요?
생성자 함수의 코드를 수정하는 것
생성자 함수 자체를 반환하는 것
새로운 인스턴스 객체를 생성하고 반환하는 것
생성자 함수의 프로토타입을 삭제하는 것
답변 1
0
function Book(){
this.point = 100;
}
Book.prototype = {
constructor: Book,
getPoint: function(){
return this.point;
},
add: function(){
console.log(12312331)
}
}
var obj = new Book();
console.log(obj.getPoint())
// 100 이 호출되고
function Book(){
this.point = 100;
}
var obj = new Book();
Book.prototype = {
constructor: Book,
getPoint: function(){
return this.point;
},
add: function(){
console.log(12312331)
}
}
console.log(obj.getPoint())
//Uncaught TypeError: obj.getPoint is not a function 이렇게 뜨네요
getPoint를 호출한 시점에 Book.prototype을 참조한다고 이해했는데 위치에 따라 왜 다르게 나오는걸까요 ..?ㅠ





