• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

prototype의 메소드 내부에 화살표 함수가 작성된 경우

22.05.26 04:42 작성 조회수 116

1

안녕하세요

강의내용을 따라하다가 한가지 질문이 생겨서 글을 씁니다

prototype에 연결한 함수, 그러니까 method안에다가 화살표함수를 사용한 경우에서 말인대요

여기서 이 method의 스코프는 인스턴스이고, 그걸 화살표 함수가 this로서 사용한다고 이해를 했습니다

그래서 debugger를 붙여서 브라우저의 개발자 모드로 확인해봤는데, 

const Point = function(){
    this.point = 100;
};

Point.prototype.getPoint = function(){      // Set an Arrow Function in a normal function that is set under "prototype"
    const add = () => this.point +20;
    console.log(add());
    [1, 2].forEach((value) => {
        console.log(this.point + value);    // the "this" of the Arrow Function takes the "this" of its scope, which is the method, "getPoint()"
    })                                      // the scope of "getPoint()" is the instance
};

new Point().getPoint();

debugger;

저기에 나오는 console.log(this.point + value)에서 this에 마우스를 가져가보면 Window라고 뜨고, 

this.point위로 마우스를 가져가 보면 undefined가 뜹니다 원래 이런건가요?

답변 0

답변을 작성해보세요.

답변을 기다리고 있는 질문이에요.
첫번째 답변을 남겨보세요!