July 27, 2020
() =>로 축약할수 있게 되었다.// 구버전
setTimeout(function(){
console.log('fuckyou')
}, 1000)
// 화살표함수
setTimeout(() => {
console.log('fuckyou')
}, 1000)
let newArr = [1,2,3,4,5].map((res, index, object) => {
return res*2;
})
console.log(newArr) // [2,4,6,8,10]this는 객체를 지칭한다. 하지만메소드를 화살표함수로 정의한다면, this는 객체가 아닌 window를 지칭하게된다.
2. 콜백함수로 일반함수를 사용했을 때, 객체 내부더라도 window를 지칭한다. 이럴경우, 이전까지는 .bind(this)를 통해 해결해왔지만, 화살표함수로 해결 가능해졌다.
실제로, React에서 컴포넌트 내부의 메소드를 호출하려 할 때,
this.method처럼 직접연결할 경우, 뒤에.bind(this)를 써줬어야 했는데, 직접연결이 아닌() => {this.method}는 써줄 필요가 없었다.
const myObj = {
name : 'apple',
arrowThis : () => { // 객체 내 화살표함수
console.log(this) // window
},
normalThis(){ // 객체 내 일반메소드
console.log(this) // myObj
},
arrowCallback(){ // 객체 내 화살표 콜백함수
setTimeout(() => {
console.log(this) // myObj
}, 100)
},
normalCallback(){
setTimeout(function(){ // 객체 내 일반 콜백함수
console.log(this) // window
}, 100)
},
};
myObj.arrowThis();
myObj.normalThis();
myObj.arrowCallback();
myObj.normalCallback();