๐ก ์๋ฐ์คํฌ๋ฆฝํธ ํจ์ ์์๋ณด๊ธฐ
1. ๋ด๋ถํจ์
2. ์ฝ๋ฐฑํจ์
3. ๋ฆฌํดํจ์
4. ํด๋ก์ฆํจ์
5. ๋๋ค
6. ๋๊ธฐ์ ๋น๋๊ธฐ ํจ์
1. ๋ด๋ถ ํจ์
let f1 = function(){
console.log('1');
let f2 = function(){
console.log('2');
}
f2();
}
f1();
f1 = function(){
console.log('3');
let f2 = function(){
console.log('4');
}
f2();
}
f1();
// 1
// 2
// 3
// 4
- ํจ์ ๋ด์์ ๋ ๋ค๋ฅธ ํจ์๋ฅผ ์ ์ธํ๊ณ ํธ์ถํ๋ ํํ
2. ์ฝ๋ฐฑํจ์
// ์ฝ๋ฐฑํจ์
// fc =
let f1 = function(fc){
console.log('1');
fc(); // f1 ํจ์์ ์ธ์ fc๊ฐ ์คํ๋๋ค๋ ์๋ฏธ
}
let f2 = function(){
console.log('2');
}
// ๋ฐฉ๋ฒ 1
f1(f2); // 1 , 2
// ๋ฐฉ๋ฒ 2
f1(function(){
console.log('3');
}); // 1 , 3
// ๋ฐฉ๋ฒ 3
f1(() => {
console.log('4');
}); // 1 , 4
- ํจ์์ ๋งค๊ฐ๋ณ์(์ธ์)๊ฐ ํจ์์ธ ํํ
- ๋ถ๋ชจ ํจ์๊ฐ ํธ์ถ๋๋ฉด ์์ ํจ์๋ ๋ถ๋ฌ์ง๋ ํํ
3. ๋ฆฌํดํจ์
// ๋ฆฌํดํจ์
let f1 = function(){
console.log('1');
let f2 = function(){
console.log('2');
}
return f2; // return f2();
}
f1()();
let f3 = function(){
console.log('3');
return function(){
console.log('4');
};
}
f3()();
- ๋ฆฌํด ๊ฐ์ด ํจ์์ธ ํํ
- ๋ฆฌํด๋๋ ํจ์๊ฐ ๋ถ๋ชจ ํจ์ ๋ด์ ์ ์ธ๋์ด ์๊ฑฐ๋ ๋ฆฌํด๋๋ ๋์์ ์ฆ์ ์คํ๋๋ ํจ์ ํํ
- ํจ์๊ฐ ๋ฆฌํด๋๋ ํจ์๋ฅผ ํธ์ถํ ๋ f1()() ์ด๋ฐ ๋ฐฉ์์ผ๋ก ํธ์ถํด์ผ ํ๋ค.
์๊ธฐ ์์ ์ ํธ์ถํด์ ๋ฆฌํดํ๋ ํจ์
(function(){
console.log('1');
return function(){
console.log('2');
}
})()();
4. ํด๋ก์ ํจ์
// ํด๋ก์ฆ ํจ์
let f1 = function(){
let a = 100;
return function(){
console.log(a);
}
};
f1()();
- ์ง์ญ๋ณ์๊ฐ ์ฌ๋ผ์ง์ง ์๊ณ ๊ณ์ ์ฌ์ฉํ ์ ์๋ ์ํ๋ก ๋จ์ ์๋ค.
- f1() ์ด ํธ์ถ๋๋ฉด f1()์ function()์ ๋ฆฌํดํ๊ณ ์๋ฉธํ๊ธฐ ๋๋ฌธ์ ์ง์ญ๋ณ์ a ๋ฅผ ๋์ด์ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
- ๊ทธ๋ฌ๋, f1() ๋ด์์ ๋ฆฌํด๋๋ function() ํจ์ ๋ด์์ a๋ฅผ ์ฌ์ฉํ๊ณ ์๊ณ , function() ํจ์๋ ์์ ์ค์ฝํ์ธ f1() ํจ์๊ฐ ์๋ฉธํ๋๋ผ๋ ์์ ์ด ์ ์ธ๋ ํ๊ฒฝ์ ๊ธฐ์ตํ๊ธฐ ๋๋ฌธ์ (=> ํด๋ก์ ํจ์ ํน์ง) f1() ๋ฐ์์ function()์ ํธ์ถํ๋๋ผ๋ ๊ทธ ํ๊ฒฝ์ ์ ๊ทผํ ์ ์๊ณ ์ง์ญ๋ณ์ a๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
- ์ต์ ์ํ๋ฅผ ์ ์งํ๋๋ฐ ์ ์ฉํ๋ค.
5. ๋๋ค
// ์์ 1
let f1 = function(a){
console.log(a);
}
f1(1);
f1 = (a) => {
console.log(a);
}
f1(2);
// (a) => { (b) => {} }
let f2 = (a) => {
console.log(a);
let f2 = (b) => {
console.log(b);
}
f1(a+3);
}
f2(3);
// 1
// 2
// 3
// 6
// ์์ 2
((a) => {
console.log(a);
let f2 = (b) => {
console.log(b);
};
return f2(a + 3);
})(3);
// 3
// 6
(function (a) {
console.log(1);
(function (b) {
console.log(b);
})(a);
})(4);
// 1
// 4
๋๋ค ํํ
// ๊ธฐ๋ณธํ
((a) => {
((b) => {
});
})();
// ๋ฐฉ๋ฒ 1
(function (a) {
(function (b) {
});
})();
// ๋ฐฉ๋ฒ 2
let f1 = function(fc){
fc(10);
}
f1((( a ) => {
(( b ) => {
console.log(a+b);
})(20)
}));
// ๋ฐฉ๋ฒ 3
let f1 = () => {
return 100;
}
let f2 = () => 100;
let f3 = () => {
return console.log('1');
}
let f4 = () => console.log('1');
f4(); // 1
์ด์ค์ ๋ก์ฐํจ์
// ์ด์ค ์ ๋ก์ฐ ๊ธฐ๋ณธํ
let t = () => () => 100;
// ๋ณํ 1
t = () => () => {
return 100;
}
// ๋ณํ 2
t = () => {
return () => {
return 100
}
}
console.log(t()()); // 100
// ๋ณํ 3
t = (a) => {
return () => {
return 100 + a;
}
}
console.log(t(10)()); // 110
// ๋ณํ 4
t = (a) => {
return (b) => {
return a + b;
}
}
console.log(t(10)(20)); // 30
// ๋ณํ 5
t = (a) => {
return (b) => a + b;
}
// ๋ณํ 6
t = (a) => {
return (b) => a + b;
}
t = (a) => (b) => a + b;
t = a => b => a + b; // ์ธ์๊ฐ 1๊ฐ์ผ ๋๋ ๊ดํธ ์๋ต ๊ฐ๋ฅ
// ๋ณํ 7
t = (a, c) => (b, d) => a * b + c * d;
console.log(t(1, 2)(3, 4)); // 11
6. ๋๊ธฐ์ ๋น๋๊ธฐ
๐ก ๋๊ธฐ ๋ฐฉ์ (Synchronous)
- ์์ฒญ์ ๋ณด๋ด๊ณ ์คํ์ด ๋๋๋ฉด ๋ค์ ๋์์ ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌํ๋ ๋ฐฉ์
- ์ฌ๋ฌ ์์ฒญ์ ๋์์ ์ฒ๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ์ ํจ์จ์ฑ์ด ๋จ์ด์ง๋ค.
- ๋ํ์ ์ธ ์๋ก ์ฝ์ผํฐ์ ์ผ์ฒ๋ฆฌ ๋ฐฉ์์ด ์๋ค. ์ฝ์ผํฐ์ ์ง์์ ํ ์๋์ ์๋๋ฅผ ๋ง์น ํ์ ๋ค๋ฅธ ์๋์ ์๋ํ ์ ์๋ค.
๐ก ๋น๋๊ธฐ ๋ฐฉ์ (Asynchronous)
- ์์ฒญ์ ๋ณด๋ด๊ณ ํด๋น ๋์์ ์ฒ๋ฆฌ ์ฌ๋ถ์ ์๊ด์์ด ๋ค์ ์์ฒญ์ด ๋์ํ๋ ๋ฐฉ์
- ์์ ์ด ์๋ฃ๋๋ ์๊ฐ์ ๊ธฐ๋ค๋ฆด ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์์์ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
- ๋ํ์ ์ธ ์๋ก ์ด๋ฉ์ผ์ด ์๋ค. ํ ์ฌ๋์๊ฒ ๋ฉ์ผ์ ๋ณด๋์ ๋ ๋ต๋ณ์ ๋ฐ์ง ์๊ณ ๋ ๋ค๋ฅธ ์ฌ๋์๊ฒ ๋ฉ์ผ์ ๋ณด๋ผ ์ ์๋ค.
setTimeout
// ๋น๋๊ธฐ ํจ์
console.log('1');
setTimeout(
function () {
console.log('3');
},
2000
)
console.log('2');
// 1 -> 2 -> 3
- setTimeout ์ผ๋ก 2์ด ๋ค์ ํจ์๋ฅผ ์ฝํ๋ค.
- ์คํ ์ 1์ด ๋จผ์ ์ถ๋ ฅ๋๊ณ setTimeout ํจ์๊ฐ ์คํ๋์ด 2์ด๋ฅผ ๊ธฐ๋ค๋ฆผ๊ณผ ๋์์ 2๊ฐ ์ถ๋ ฅ๋ ๋ค์ 2์ด๊ฐ ๋ค ํ๋ฅด๋ฉด 3์ด ์ถ๋ ฅ๋๋ค.
setInterval
let id = setInterval(
() => {
console.log('3');
},
2000
);
console.log('2');
// 2 -> 3 -> 3 -> 3 -> 3 ....
- setInterval ์ n ์ด๋ง๋ค ํจ์๋ฅผ ์ฝํ๋ค.
- setTimeout ์ผ๋ก ํจ์๋ฅผ ์ข ๋ฃํด์ผ ํ๋ค.
setTimeout & setInterval
let id = setInterval(
() => {
console.log('3');
},
2000
);
console.log('2');
setTimeout(
() => {
clearInterval(id);
console.log('exit');
},
2000
)
console.log('2');
// 2 -> 2 -> 2-> 3 -> exit
- setInterval ํจ์๋ฅผ ์คํํ๊ณ setTimeout ์ผ๋ก setInterval ํจ์๋ฅผ ์ข ๋ฃํ๋ค.
์ฆ์ ์คํ ํจ์
// ์ฆ์ ์คํ ํจ์
for(let i=0; i<3; i++){
(function(x){
setTimeout(
()=>{ console.log(i); },
1000
)
})(i)
}
// 0 -> 1 -> 2
- ์ฆ์ ์คํ ํจ์๋ก 1์ด๋ง๋ค ์คํ๋๋ค.
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
addEventListener ์ด๋ฒคํธ (input & change) (0) | 2022.04.25 |
---|---|
[JavaScript] ์๋ฐ์คํฌ๋ฆฝํธ Prototype (0) | 2021.09.30 |
[JavaScript] ๋๋ฒ๊ฑฐ ์๋ฌ ํด๊ฒฐ (0) | 2021.09.30 |
[JavaScript] ์๋ฐ ์คํฌ๋ฆฝํธ ๊ธฐ๋ณธ ํจ์ (0) | 2021.09.29 |
[JavaScript] ์๋ฐ ์คํฌ๋ฆฝํธ ๊ธฐ์ด (0) | 2021.09.28 |
๋๊ธ