Web/javascript
화살표 함수 써야 하는 이유
만년초보
2023. 11. 10. 10:21
반응형
함수내에 콜백 함수에서 this는 해당 함수의 lexical scope를 참조하지 못한다.
function a () {
var foo = 'var';
console this.foo : print var
setTimeOut(function() {
console this.foo : print undefined
})
}
function a () {
var foo = 'var';
console this.foo : print var
setTimeOut(() => {
console this.foo : print var
})
}
즉 this를 lexical scope 환경에서 제대로 활용하기 위해선 화살표 함수가 좋다!
반응형