let dog = {
name: "Spot",
numLegs: 4,
sayLegs: function() {return "This dog has " + this.numLegs + " legs.";}
};
dog.sayLegs();
얘는 잘굴러가고
let dog = {
name: "Spot",
numLegs: 4,
sayLegs: () => "This dog has " + this.numLegs + " legs."
};
dog.sayLegs();
얘는 Cannot read properties of undefined 뜨는데 왜그런거임?
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/Arrow_functions
ㄳㄳ
위는 메서드 호출 전까지 this가 정해지지 않음. 그래서 . 앞의 객체인 dog가 런타임에 this로 바인딩됨. 아래는 this가 만들때부터 외부컨텍스트인 window로 정해짐. 근데 window에는 numLegs가 없으니까 undefined라고 뜨는거고
거의 맞긴한데, js에서 this는 항상 런타임 바인딩임
아래는 스코프 체이닝으로 this 찾은거고
ㄱㅅㄱㅅ 이렇게 또 알아가네