๐Ÿฏ ์ฝ”์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ - 04 ์ฝœ๋ฐฑ ํ•จ์ˆ˜ (1)

modoleeยท2020๋…„ 9์›” 19์ผ
1
post-thumbnail

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ž€?

  • callback function
    • call : '๋ถ€๋ฅด๋‹ค', 'ํ˜ธ์ถœ(์‹คํ–‰)ํ•˜๋‹ค'
    • back : '๋’ค๋Œ์•„์˜ค๋‹ค', '๋˜๋Œ๋‹ค'
    • ๋‘ ๋‹จ์–ด์˜ ํ•ฉ์„ฑ์–ด๋กœ '๋˜๋Œ์•„ ํ˜ธ์ถœํ•ด๋‹ฌ๋ผ'๋Š” ๋ช…๋ น
  • ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ํ•จ์ˆ˜
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋„˜๊ฒจ ๋ฐ›์€ ์ฝ”๋“œ๋Š” ์ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•„์š”์— ๋”ฐ๋ผ ์ ์ ํ•œ ์‹œ์ ์— ์‹คํ–‰
    • ์–ด๋–ค ํ•จ์ˆ˜ X๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ 'ํŠน์ • ์กฐ๊ฑด์ผ ๋•Œ ํ•จ์ˆ˜ Y๋ฅผ ์‹คํ–‰ํ•ด์„œ ๋‚˜์—๊ฒŒ ์•Œ๋ ค๋‹ฌ๋ผ'๋Š” ์š”์ฒญ์„ ํ•จ๊ป˜ ๋ณด๋ƒ„
    • ํ•จ์ˆ˜ X๋Š” ํ•ด๋‹น ์กฐ๊ฑด์„ ์Šค์Šค๋กœ ์ฒดํฌํ•ด์„œ Y๋ฅผ ์ง์—… ํ˜ธ์ถœ

์ œ์–ด๊ถŒ

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ์ฝ”๋“œ(ํ•จ์ˆ˜ ๋˜๋Š” ๋ฉ”์„œ๋“œ)์—๊ฒŒ ์ธ์ž๋กœ ๋„˜๊ฒจ์คŒ์œผ๋กœ์จ ๊ทธ ์ œ์–ด๊ถŒ๋„ ํ•จ๊ป˜ ์œ„์ž„

ํ˜ธ์ถœ ์‹œ์ 

์˜ˆ์ œ - setInterval

let count = 0;
const timer = setInterval(function () {
  console.log(count);
  if (++count > 4) clearInterval(timer);
}, 300);

// ์‹คํ–‰ ๊ฒฐ๊ณผ
// 0
// 1
// 2
// 3
// 4
  • 300ms ๋งˆ๋‹ค ์นด์šดํŒ… ๋œ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์นด์šดํŠธ๊ฐ€ 4๋ฅผ ๋„˜์œผ๋ฉด ํƒ€์ด๋จธ๋ฅผ ์ดˆ๊ธฐํ™” ์‹œ์ผœ ์ข…๋ฃŒ
  • 300ms ๋งˆ๋‹ค ์ฃผ๊ธฐ์ ์œผ๋กœ callback ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ

์˜ˆ์ œ - setInterval cbFunc ๋ช…์‹œ์  ํ‘œํ˜„

let count = 0;
const cbFunc = function () {
  console.log(count);
  if (++count > 4) clearInterval(timer);
}
let timer = setInterval(cbFunc, 300);

// ์‹คํ–‰ ๊ฒฐ๊ณผ
// 0
// 1
// 2
// 3
// 4
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋ณด๋‹ค ๋ช…์‹œ์ ์œผ๋กœ ํ‘œํ˜„ (cbFunc)
  • cbFunc, setInterval์˜ ํ˜ธ์ถœ ์ฃผ์ฒด์™€ ์ œ์–ด๊ถŒ ๋น„๊ต

์ธ์ž

์˜ˆ์ œ - Array.prototype.map

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]

์˜ˆ์ œ - Array.prototype.map ์ธ์ž ์ˆœ์„œ ๋ณ€๊ฒฝ

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]
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•  ๋•Œ ๋„˜๊ธฐ๋Š” ์ธ์ž ๊ฐ’์€ ์ด๋ฆ„์— ๊ด€๊ณ„ ์—†์ด ์ˆœ์„œ๋Œ€๋กœ ๊ฐ’์ด ์ „๋‹ฌ
  • ์ „๋‹ฌ๋˜๋Š” ์ธ์ž์˜ ์ˆœ์„œ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ์ฃผ์ฒด๊ฐ€ ๋˜๋Š” ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ)๊ฐ€ ๊ฒฐ์ •

this

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋„ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์œผ๋กœ this๊ฐ€ ์ „์—ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ
  • ํ•˜์ง€๋งŒ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ ๋ฐ›์„ ์ฝ”๋“œ์—์„œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ๋ณ„๋„๋กœ this๊ฐ€ ๋  ๋Œ€์ƒ์„ ์ง€์ •ํ•œ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ๋Œ€์ƒ์„ ์ฐธ์กฐ

์˜ˆ์ œ - Array.prototype.map ๊ตฌํ˜„

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]
  • thisArg๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ๊ฐ’์„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๋กœ ์ง€์ • ์—†๋Š” ๊ฒฝ์šฐ, ์ „์—ญ ๊ฐ์ฒด(window)๋ฅผ this๋กœ ์ง€์ •

์ฒ˜์Œ ์˜ˆ์ œ๋ฅผ ๋ดค์„ ๋•Œ์—๋Š” this๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์ง€์ •ํ•˜๋ฉด map์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋„ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์ธ ์ค„ ์•Œ์•˜๋Š”๋ฐ, ์‹คํ–‰์„ ํ•ด๋ณด๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์„œ ์กฐ๊ธˆ ๋‹นํ™ฉ ํ–ˆ๋‹ค. ๊ณฐ๊ณฐํžˆ ์ƒ๊ฐ์„ ํ•ด๋ณด๋‹ˆ thisArg๋กœ ๋„˜๊ธฐ๋Š” ๊ฒƒ์€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด์ง€ map ๋ฉ”์„œ๋“œ์˜ this๋ฅผ ์ง€์ •ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ ๊ฐ’์—๋Š” ์˜ํ–ฅ์ด ์—†๋Š”๊ฒŒ ๋งž๋‹ค.

์˜ˆ์ œ - addEventListener

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 ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚ด
profile
๊ธฐ์ดˆ๊ฐ€ ํƒ„ํƒ„ํ•œ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž๋ฅผ ๊ฟˆ๊ฟ‰๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€