• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

원본 생성자함수의 prototype안 메소드를 삭제해도 인스턴스에 할당된 메소드는 이와관계없이 동작이되는건가요?

21.06.16 21:43 작성 조회수 28

1

삭제된 글입니다

답변 1

답변을 작성해보세요.

0

rudals8920님의 프로필

rudals8920

2021.07.14

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을 참조한다고 이해했는데 위치에 따라 왜 다르게 나오는걸까요 ..?ㅠ