โ ์ฝ๋ฐฑ ํจ์๋ ๋ค๋ฅธ ์ฝ๋(ํจ์ ๋๋ ๋ฉ์๋)์๊ฒ ์ธ์๋ก ๋๊ฒจ์ค์ผ๋ก์จ ๊ทธ ์ ์ด๊ถ๋ ํจ๊ป ์์ํ ํจ์
์์ 4-2) setInterval
var count = 0;
// ์ฝ์์ฐฝ์ 0.3์ด์ ํ ๋ฒ์ฉ ์ซ์๊ฐ 0๋ถํฐ 1์ฉ ์ฆ๊ฐํ๋ฉด์ ์ถ๋ ฅ, 4๊ฐ ์ถ๋ ฅ๋ ์ดํ ์ข
๋ฃ
var cbFunc = function() {
console.log(count);
if (++count > 4) clearInterval(timer);
};
var timer = serInterval(cbFunction, 300);
setInterval
: ์ฝ๋ฐฑ ํจ์๋ฅผ ๋๊ฒจ๋ฐ์ ์ฝ๋
cbFunc
: ์ฝ๋ฐฑ ํจ์
setInterval
์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก cbFunc
ํจ์๋ฅผ ๋๊ฒจ์คsetInterval
์ด ์ค์ค๋ก์ ํ๋จ์ ๋ฐ๋ผ ์ ์ ํ ์์ (0.3์ด๋ง๋ค) ์ cbFunc
ํจ์๋ฅผ ์คํํ 4-1) ์ฝ๋ ์คํ ๋ฐฉ์๊ณผ ์ ์ด๊ถ
code | ํธ์ถ ์ฃผ์ฒด | ์ ์ด๊ถ |
---|---|---|
cbFunc(); | ์ฌ์ฉ์ | ์ฌ์ฉ์ |
setInterval(cbFunc, 300); | setInterval | setInterval |
โ ์ฝ๋ฐฑ ํจ์์ ์ ์ด๊ถ์ ๋๊ฒจ๋ฐ์ ์ฝ๋๋ ์ฝ๋ฐฑ ํจ์์ ํธ์ถ ์์ ์ ๋ํ ์ ์ด๊ถ์ ๊ฐ์ง
Array์ prototype์ ๋ด๊ธด map ๋ฉ์๋์ ๊ตฌ์กฐ
Array.prototype.map(callback[, thisArg])
callback: function(currentValue, index, array)
map
๋ฉ์๋thisArg
๋ฅผ ์๋ตํ ๊ฒฝ์ฐ, ์ผ๋ฐ์ ์ธ ํจ์๊ณผ ๊ฐ์ด ์ ์ญ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ ๋๋ค.โ map
๋ฉ์๋๋ ๋ฉ์๋์ ๋์์ด ๋๋ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ค์ ์ฒ์๋ถํฐ ๋๊น์ง ํ๋์ฉ ๊บผ๋ด์ด ์ฝ๋ฐฑ ํจ์๋ฅผ ๋ฐ๋ณต ํธ์ถํ๊ณ , ์ฝ๋ฐฑ ํจ์์ ์คํ ๊ฒฐ๊ณผ๋ค์ ๋ชจ์ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑ
map
๋ฉ์๋์ ๋์์ด ๋๋ ๋ฐฐ์ด ์์ฒด์์ 4-3) Array.prototype.map
var 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]
์์ 4-4) Array.prototype.map - ์ธ์์ ์์๋ฅผ ์์๋ก ๋ฐ๊พธ์ด ์ฌ์ฉ
var 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]
map
๋ฉ์๋๋ฅผ ํธ์ถํด์ ์ํ๋ ๋ฐฐ์ด์ ์ป์ผ๋ ค๋ฉด map
๋ฉ์๋์ ์ ์๋ ๊ท์น์ ๋ฐ๋ผ ํจ์๋ฅผ ์์ฑํด์ผ ํ๋ค.map
๋ฉ์๋์ ์ ์๋ ๊ท์น์๋ ์ฝ๋ฐฑ ํจ์์ ์ธ์๋ก ๋์ด์ฌ ๊ฐ๋ค ๋ฐ ๊ทธ ์์๋ ํฌํจ๋์ด ์๋ค.map
๋ฉ์๋map
๋ฉ์๋๊ฐ ์ด๋ฐฑ ํจ์๋ฅผ ํธ์ถํ ๋ ์ธ์์ ์ด๋ค ๊ฐ๋ค์ ์ด๋ค ์์๋ก ๋๊ธธ ๊ฒ์ธ์ง๊ฒ ์ ์ ์ผ๋ก map
๋ฉ์๋์๊ฒ ๋ฌ๋ฆผโ ์ฝ๋ฐฑ ํจ์์ ์ ์ด๊ถ์ ๋๊ฒจ๋ฐ์ ์ฝ๋๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ ๋ ์ธ์์ ์ด๋ค ๊ฐ๋ค์ ์ด๋ค ์์๋ก ๋๊ธธ ๊ฒ์ธ์ง์ ๋ํ ์ ์ด๊ถ์ ๊ฐ์ง๋ค.
์์ 4-6) ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์์์ this
// (1) setTimeout
setTimeout(function () { console.log(this); }, 300); // Window{ ... }
// (2) forEach
[1, 2, 3, 4, 5].forEach(function (x) {
console.log(this); // Window{ ... }
});
// (3) addEventListener
document.body.innerHTML += '<button id="a">Click</button>';
document.body.querySelector('#a').addEventListener('Click', function (e) {
console.log(this, e); // <button id="a">Click</button> MouseEvent { isTrusted: true, ... }
}
);
setTimeout
ํจ์๋ 0.3์ด ๋งํผ ์๊ฐ ์ง์ฐ์ ํ ๋ค ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํ, 0.3์ด ๋ค ์ ์ญ๊ฐ์ฒด๊ฐ ์ถ๋ ฅ
โ setTimeout
์ ๋ด๋ถ์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ ๋, call ๋ฉ์๋์ ์ฒซ ๋ฒ์ฌ ์ธ์์ ์ ์ญ๊ฐ์ฒด๋ฅผ ๋๊ธฐ๊ธฐ ๋๋ฌธ์ ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์์ this๊ฐ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
forEach
๋ฉ์๋๋ ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ์์์๋ถํฐ ์ฐจ๋ก๋ก ํ๋์ฉ ๊บผ๋ด์ด ๊ทธ ๊ฐ์ ์ฝ๋ฐฑ ํจ์์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ผ์ ํจ์๋ฅผ ์คํ, ์ ์ญ๊ฐ์ฒด์ ๋ฐฐ์ด์ ๊ฐ ์์๊ฐ ์ด 5ํ ์ถ๋ ฅ
โ ์ฌ๊ธฐ์์ forEach
๋ 4-2-5์ ์ '๋ณ๋์ ์ธ์๋ก this๋ฅผ ๋ฐ๋ ๊ฒฝ์ฐ'์ ํด๋นํ์ง๋ง ๋ณ๋์ ์ธ์๋ก this๋ฅผ ๋๊ฒจ์ฃผ์ง ์์๊ธฐ ๋๋ฌธ์ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
addEventListener
๋ ์ง์ ํ HTML ์๋ฆฌ๋จผํธ์ 'click' ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋๋ง๋ค ๊ทธ ์ด๋ฒคํธ ์ ๋ณด๋ฅผ ์ฝ๋ฐฑ ํจ์์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ผ์ ํจ์๋ฅผ ์คํ, ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์์ ์ง์ ํ ์๋ฆฌ๋จผํธ์ ํด๋ฆญ ์ด๋ฒคํธ์ ๊ดํ ์ ๋ณด๊ฐ ๋ด๊ธด ๊ฐ์ฒด ์ถ๋ ฅ
โ addEventListener
๋ ๋ด๋ถ์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ ๋ call ๋ฉ์๋์ ์ฒซ ๋ฒ์ฌ ์ธ์์ addEventListener
๋ฉ์๋์ this๋ฅผ ๊ทธ๋๋ก ๋๊ธฐ๋๋ก ์ ์๋ผ ์๊ธฐ ๋๋ฌธ์ ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์์์ this๊ฐ addEventListener
๋ฅผ ํธ์ถํ ์ฃผ์ฒด์ธ HTML ์๋ฆฌ๋จผํธ๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
setTimeout
ํจ์์ forEach
๋ฉ์๋๋ ๊ทธ ๋ด๋ถ์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ ๋ ๋์์ด ๋ this๋ฅผ ์ง์ ํ์ง ์๋๋ค. ๋ฐ๋ผ์ ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์์์ this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ
addEventListener
๋ฉ์๋๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ ๋ ์์ ์ this๋ฅผ ์์ํ๋๋ก ์ ์
โ ๋ฉ์๋๋ช
์ ์ (.) ์๋ถ๋ถ์ด this๊ฐ ๋๋ค.
์์ 4-7) ๋ฉ์๋๋ฅผ ์ฝ๋ฐฑ ํจ์๋ก ์ ๋ฌํ ๊ฒฝ์ฐ
var obj = {
vals: [1, 2, 3],
// obj ๊ฐ์ฒด์ ๋ฉ์๋๋ก ์ ์๋ logValues
logValues: function(v, i) {
console.log(this, v, i);
}
};
// this๋ obj๋ฅผ ๊ฐ๋ฆฌํค๊ณ , ์ธ์๋ก ๋์ด์จ 1, 2๊ฐ ์ถ๋ ฅ
obj.logValues(1, 2); // {vals: Array(3), logValues: ฦ} 1 2
// logValues ๋ฉ์๋๋ฅผ forEach ํจ์์ ์ฝ๋ฐฑ ํจ์๋ก์ ์ ๋ฌ
// obj๋ฅผ this๋ก ํ๋ ๋ฉ์๋๋ฅผ ๊ทธ๋๋ก ์ ๋ฌํ ๊ฒ์ด ์๋๋ผ,
// obj.logValues๊ฐ ๊ฐ๋ฆฌํค๋ ํจ์๋ง ์ ๋ฌ, obj์ ์ง์ ์ ์ธ ์ฐ๊ด์ด ์์ด์ง
// ๋ณ๋๋ก this๋ฅผ ์ง์ ํ๋ ์ธ์๋ฅผ ์ง์ ํ์ง ์์์ผ๋ฏ๋ก ํจ์ ๋ด๋ถ์์์ this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ๋ฐ๋ผ๋ณด๊ฒ ๋๋ค.
[4, 5, 6].forEach(obj.logValues);
// Window { ... } 4 0
// Window { ... } 5 1
// Window { ... } 6 2
โ ์ด๋ค ํจ์์ ์ธ์์ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ ๋ฌํ๋๋ผ๋ ์ด๋ ๊ฒฐ๊ตญ ๋ฉ์๋๊ฐ ์๋ ํจ์์ด๋ค.
์์ 4-8) ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์ this์ ๋ค๋ฅธ ๊ฐ์ ๋ฐ์ธ๋ฉํ๋ ๋ฐฉ๋ฒ - ์ ํต์ ์ธ ๋ฐฉ์
var obj1 = {
name: 'obj1',
func: function () {
// func ๋ด๋ถ์์ self ๋ณ์์ this ๋ด๊ณ , ์ต๋ช
ํจ์๋ฅผ ์ ์ธ๊ณผ ๋์์ ๋ฐํ
var self = this;
return function () {
console.log(self.name);
};
}
};
var callback = obj1.func();
setTimeout(callback, 1000);
์์ 4-10) ์์ 4-8์ func ํจ์ ์ฌํ์ฉ
...
var obj2 = {
name: 'obj2',
func: obj1.func
};
// obj1์ func๋ฅผ ๋ณต์ฌํ obj2์ func๋ฅผ ์คํํ ๊ฒฐ๊ณผ๋ฅผ ๋ด์ ์ฝ๋ฐฑ์ผ๋ก ์ฌ์ฉ
var callback2 = obj2.func();
setTimeout(callback2, 1500);
var obj3 = { name: 'obj3' };
// obj1์ func๋ฅผ ์คํํ๋ฉด์ this๋ฅผ obj3๊ฐ ๋๋๋ก ์ง์ ํด์ ์ฝ๋ฐฑ์ผ๋ก ์ฌ์ฉ
var callback3 = obj1.func.call(obj3);
setTimeout(callback3, 2000);
์์ 4-11) ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์ this์ ๋ค๋ฅธ ๊ฐ์ ๋ฐ์ธ๋ฉํ๋ ๋ฐฉ๋ฒ - bind ๋ฉ์๋ ํ์ฉ
var obj1 = {
name: 'obj1',
func: function () {
console.log(this.name);
}
};
setTimeout(obj1.func.bind(obj1), 1000);
var obj2 = { name: obj1};
setTimeout(obj1.func.bind(obj2), 1500);
setTimeout
)addEventListener
)XMLHttpRequest
) ๋ฑ์์ 4-12) ์ฝ๋ฐฑ ์ง์ฅ ์์
setTimeout(function (name) {
var coffeeList = name;
console.log(coffeeList);
setTimeout(function (name) {
coffeeList += ', ' + name;
console.log(coffeeList);
setTimeout(function (name) {
coffeeList += ', ' + name;
console.log(coffeeList);
setTimeout(function (name) {
coffeeList += ', ' + name;
console.log(coffeeList);
}, 500, '์นดํ๋ชจ์นด');
}, 500, '์นดํ๋ผ๋ผ');
}, 500, '์๋ฉ๋ฆฌ์นด๋
ธ');
}, 500, '์์คํ๋ ์');
์์ 4-13) ๊ธฐ๋ช ํจ์๋ก ๋ณํ
var coffeeList = '';
var addEspresso = function (name) {
coffeeList = name;
console.log(coffeeList);
setTimeout(addAmericano, 500, '์๋ฉ๋ฆฌ์นด๋
ธ');
};
var addAmericano = function (name) {
coffeeList += ', ' + name;
console.log(coffeeList);
setTimeout(addLatte, 500, '์นดํ๋ผ๋ผ');
};
var addLatte = function (name) {
coffeeList += ', ' + name;
console.log(coffeeList);
setTimeout(addMocha, 500, '์นดํ๋ชจ์นด');
};
var addMocha = function (name) {
coffeeList += ', ' + name;
console.log(coffeeList);
};
setTimeout(addEspresso, 500, '์์คํ๋ ์');
โ ์ผํ์ฑ ํจ์๋ฅผ ์ ๋ถ ๋ณ์์ ํ ๋นํ๋ ๊ฒ, ์ฝ๋๋ช ์ ์ผ์ผ์ด ๋ฐ๋ผ๋ค๋ ์ผ ํ๋ฏ๋ก ์คํ๋ ค ํท๊ฐ๋ฆด ์ ์๋ค.
new Promise(function (resolve) {
setTimeout(function () {
var name = '์์คํ๋ ์';
console.log(name);
resolve(name);
}, 500)
}).then(function (prevName) {
return new Promise(function (resolve) {
setTimeout(function () {
var name = prevName + ', ์๋ฉ๋ฆฌ์นด๋
ธ';
console.log(name);
resolve(name);
}, 500)
})
}).then(function (prevName) {
return new Promise(function (resolve) {
setTimeout(function () {
var name = prevName + ', ์นดํ๋ผ๋ผ';
console.log(name);
resolve(name);
}, 500)
})
}).then(function (prevName) {
return new Promise(function (resolve) {
setTimeout(function () {
var name = prevName + ', ์นดํ๋ชจ์นด';
console.log(name);
resolve(name);
}, 500)
});
});
// ์์ ์ฝ๋์์ ๋ฐ๋ณต์ ์ธ ๋ด์ฉ์ ํจ์ํํด์ ์งง๊ฒ ํํ
var addCoffee = function (name) {
return function (prevName) {
return new Promise(function (resolve) {
setTimeout(function () {
var newName = prevName ? (prevName + ', ' + name) : name;
console.log(newName);
resolve(newName);
}, 500);
});
};
};
addCoffee('์์คํ๋ ์')()
.then(addCoffee('์๋ฉ๋ฆฌ์นด๋
ธ'))
.then(addCoffee('์นดํ๋ผ๋ผ'))
.then(addCoffee('์นดํ๋ชจ์นด'));
โ ๋น๋๊ธฐ ์์ ์ด ์๋ฃ๋ ๋ resolve/reject๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋น๋๊ธฐ ์์ ์ ๋๊ธฐ์ ํํ์ด ๊ฐ๋ฅ
var addcoffee = function (prevName, name) {
setTimeout(function () {
coffeeMaker.next(prevName ? prevName + ', ' + name: name);
}, 500);
};
var coffeeGenerator = function* () {
var espresso = yield addCoffee('', '์์คํ๋ ์');
console.log(espresso);
var americano = yield addCoffee(espresso, '์๋ฉ๋ฆฌ์นด๋
ธ');
console.log(americano);
var latte = yield addCoffee(americano, '์นดํ๋ผ๋ผ');
console.log(latte);
var mocha = yield addCoffee(mocha, '์นดํ๋ชจ์นด');
console.log(mocha);
};
var coffeeMaker = coffeeGenerater();
coffeeMaker.next()
*
๊ฐ ๋ถ์ ํจ์ : Generator
ํจ์Generator
ํจ์ ์คํํ๋ฉด, Iterator
๋ฅผ ๋ฐํIterator
: next
๋ผ๋ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค.next
๋ฉ์๋ ํธ์ถGenerator
ํจ์ ๋ด๋ถ์์ ๊ฐ์ฅ ๋จผ์ ๋ฑ์ฅ ํ๋ yield
์์ ํจ์์ ์คํ์ ๋ฉ์ถคnext
๋ฉ์๋ ํธ์ถyield
์์ ํจ์์ ์คํ์ ๋ฉ์ถคโ ๋น๋๊ธฐ ์์
์ด ์๋ฃ๋๋ ์์ ๋ง๋ค next
๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด Generator
ํจ์ ๋ด๋ถ์ ์์ค๊ฐ ์์์๋ถํฐ ์๋๋ก ์์ฐจ์ ์ผ๋ก ์งํ๋จ
var addCoffee = function (name) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(name);
}, 500)
});
};
var coffeeMaker = async function () {
var coffeeList = '';
var _addCoffee = async function (name) {
coffeeList += (coffeeList ? ',' : '') + await addCoffee(name);
};
await _addCoffee('์์คํ๋ ์');
console.log(CoffeeList);
await _addCoffee('์๋ฉ๋ฆฌ์นด๋
ธ');
console.log(CoffeeList);
await _addCoffee('์นดํ๋ผ๋ผ');
console.log(CoffeeList);
await _addCoffee('์นดํ๋ชจ์นด');
console.log(CoffeeList);
async
๋ฅผ ํ๊ธฐawait
๋ฅผ ํ๊ธฐํ๋ ๊ฒ๋ง์ผ๋ก ๋ค์ ๋ด์ฉ์ผ Promise
๋ก ์๋ ์ ํresolve
๋ ์ดํ์ ๋ค์ ์ฝ๋๋ฅผ ์งํ ํ๋ฏ๋ก Primise
์ then
๊ณผ ๋น์ทํ ํจ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.์ฐธ๊ณ
- ์ฝ์ด ์๋ฐ์คํฌ๋ฆฝํธ - 04 ์ฝ๋ฐฑ ํจ์
์ฌ๊ธฐ๊ฐ ๊ทธ ์๋ฌธ๋ ์ฝ๋ฐฑํจ์ ์ชฝ์ง๊ฒ ๊ฐ์ํด์ฃผ๋ ๊ณณ์ธ๊ฐ์~??