다른 코드의 인자로 넘겨주는 함수
콜백함수를 넘겨받은 코드는 콜백 함수를 필요에 따라 적절한 시점에 시행한다.
콜백함수는 제어권과 관련이 깊다.
callback => 되돌아 호출해달라는 명령
어떤 함수 x를 호출하면서 '특정 조건일 때 함수 y를 실행해서 나에게 알려달라'는 요청을 함께 보낸다.
이 요청을 받은 함수 x의 입장에서는 해당 조건이 갖춰졌는지 여부를 스스로 판단하고 y를 직접 호출한다.
이처럼 콜백 함수는 다른 코드에게 인자로 넘겨줌으로써 그 제어권도 함께 위임한 함수이다.
콜백 함수를 위임받은 코드는 자체적인 내부 로직에 의해 이 콜백함수를 적절한 시점에 실행할 것이다.
콜백 함수는 다른 코드(함수 또는 메서드)에게 인자로 넘겨줌으로써 그 제어권도 함께 위임
let count = 0;
const timer = setInterval(function () {
console.log(count);
if (++count > 4) clearInterval(timer);
}, 300);
// 실행 결과
// 0
// 1
// 2
// 3
// 4
let count = 0;
const cbFunc = function () {
console.log(count);
if (++count > 4) clearInterval(timer);
}
let timer = setInterval(cbFunc, 300);
// 실행 결과
// 0
// 1
// 2
// 3
// 4
const newArr = [10, 20, 30].map(function (currentValue, index) {
console.log(currentValue, index);
return currentValue + 5;
});
console.log(newArr);
// 실행 결과
// 10 0
// 20 1
// 30 2
// [15, 25, 35]
const newArr = [10, 20, 30].map(function (index, currentValue) {
console.log(index, currentValue);
return currentValue + 5;
});
console.log(newArr);
// 실행 결과
// 10 0
// 20 1
// 30 2
// [5, 6, 7]
Array.prototype.map = function (callback, thisArg) {
let mappedArr = [];
for (let i = 0; i < this.length; i++) {
const mappedValue = callback.call(thisArg || window, this[i], i, this);
mappedArr[i] = mappedValue;
}
return mappedArr;
}
const result = [10, 20, 30].map(function (value, index) {
console.log(this, value, index);
return value + 100;
});
console.log(result);
// 실행 결과
// Window 10 0
// Window 20 1
// Window 30 2
// [110, 120, 130]
const anotherResult = [10, 20, 30].map(function (value, index) {
console.log(this, value, index);
return value + 100;
}, [1, 2, 3]);
console.log(anotherResult);
// 실행 결과
// [1, 2, 3] 10 0
// [1, 2, 3] 20 1
// [1, 2, 3] 30 2
// [110, 120, 130]
const cbFunc = function () {
console.log(this);
};
setTimeout(cbFunc, 300);
// 실행 결과
// Window
const arr = [1, 2, 3, 4, 5];
arr.forEach(cbFunc);
// 실행 결과
// Window
// Window
// Window
// Window
// Window
arr.forEach(cbFunc, arr);
// 실행 결과
// [1, 2, 3, 4, 5]
// [1, 2, 3, 4, 5]
// [1, 2, 3, 4, 5]
// [1, 2, 3, 4, 5]
// [1, 2, 3, 4, 5]
document.body.innerHTML += '<button id="a">클릭</button>';
document.body.querySelector('#a')
.addEventListener('click', cbFunc);
// 실행 결과
// <button id="a">클릭</button>
setTimeout
내부에서 콜백 함수를 호출할 때 call 메서드의 첫번째 인자에 전연 객체를 넘기기 때문에 콜백 함수 내부에서의 this는 전역 객체를 가리킴
forEach
별도의 인자로 this를 받는 경우. 콜백 함수 다음 인자로 this로 지정할 객체를 넘기지 않은 경우에는 this가 전역 객체를 가리키고, this로 지정할 객체를 넘긴 경우에는 this가 해당 객체를 가리킴
addEventListener
내부에서 콜백 함수를 호출 할 때 call 메서드의 첫번째 인자에 addEventListener 메서드의 this를 그대로 넘기도록 정의돼 있기 때문에 콜백 함수 내부에서 this가 addEventListener를 호출 한 주체 HTML 엘리먼트를 가리킴