강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

Hyobin Kim님의 프로필 이미지
Hyobin Kim

작성한 질문수

모던 자바스크립트(ES6+) 기본

4. 화살표 함수와 인스턴스, 화살표 함수 특징

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

작성

·

217

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가 뜹니다 원래 이런건가요?

답변

답변을 기다리고 있는 질문이에요
첫번째 답변을 남겨보세요!
Hyobin Kim님의 프로필 이미지
Hyobin Kim

작성한 질문수

질문하기