강의

멘토링

커뮤니티

Inflearn Community Q&A

hyobinkim's profile image
hyobinkim

asked

Modern JavaScript (ES6+) Basics

4. Arrow Functions and Instances, Arrow Function Features

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

Written on

·

224

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

es6javascript

Answer

This question is waiting for answers
Be the first to answer!
hyobinkim's profile image
hyobinkim

asked

Ask a question