- 콜백 함수(callback function)를 인자로 받는 메서드 중 일부는 this로 지정할 객체를 표시해 직접 인자로 지정해줄 수 있다.
콜백 함수와 함께 thisArg를 인자로 받는 메서드
Array.prototype.forEach(callback[, thisArg]) // 콜백함수를 넣고 지정한 마지막인자는 this가 됨
Array.prototype.map(callback[, thisArg])
Array.prototype.filter(callback[, thisArg])
Array.prototype.some(callback[, thisArg])
Array.prototype.evert(callback[, thisArg])
Array.prototype.find(callback[, thisArg])
Array.prototype.findIndex(callback[, thisArg])
Array.prototype.flatMap(callback[, thisArg])
Array.prototype.from(arrayLike[, callback[, thisArg]])
Set.prototype.forEach(callback[, thisArg])
Map.prototype.forEach(callback[, thisArg])
var report = {
sum: 0,
count: 0,
add: function () {
var args = Array.prototype.slice.call(arguments);
args.forEach(function (entry) {
this.sum += entry;
++this.count;
}, this); // 여기 두번째 인자로 전해준 this가 콜백함수에서 쓰일 this값이 된다..(헷갈려ㅠ)
},
average: function () {
return this.sum / this.count;
}
};