let onComplete = () => { console.log('๋๋ง๊ณจ'); };
class Welcome {
to(onComplete) {
onComplete();
}
}
const welcome = new Welcome();
welcome.to(onComplete); //๋๋ง๊ณจ
์ต๋ช ํจ์
ํ์ ) var(ํน์ let) ๋ณ์๋ช = function(๋งค๊ฐ๋ณ์๋ช : ์๋ฃํ) {
์ฒ๋ฆฌ๊ตฌ๋ฌธ
}
๋ณ์๊ฐ ํจ์์ ์ด๋ฆ์ ๋์ ํ๊ฒ ๋๊ณ , ์ด ๋ณ์๋ฅผ ์ฌ์ฉํด์ ํจ์์ ๊ธฐ๋ฅ์ ์คํํ ์ ์๋ค.
์๋ฐ์คํฌ๋ฆฝํธ, ํจ์๋ฅผ ๋ง๋ค๋ ์ฌ์ฉํ๋ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ
var pow_ = function(x:number){ //function pow_(x:number)
console.log('x=>'+x);
return x*x;
}
console.log(pow_(10));
/*
๋งค๊ฐ๋ณ์์ ์๋ฃํ์ ์ฐ์ง ์์ผ๋ฉด, ์ด๋ค ์๋ฃํ์ด๋ ๋ฐ์ ์ ์๋ any(default)
ํ์ง๋ง ๊ถ์ฅํ์ง ์๋๋ค. ์๋ฃํ์ ๋ช
์ํ์
*/
const test_=x=>x*x;
console.log(test_(20));
ํ์ดํ ํจ์์ ํธ์ถ
ํ์ดํ ํจ์๋ ์ต๋ช ํจ์
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ํญ์ ํจ์ ํํ์์ผ๋ก ๋ณ์๋ก ์ ์ธํ๊ฑฐ๋, ์ฝ๋ฐฑํจ์์ ํ์์ผ๋ก ์ฌ์ฉ
// ๋ณ์ ์ ์ธ
const logging = (x) => console.log(x)
logging('Hello world')
// callback
const info = ['name', 'age', 'address']
info.map((data) => {
console.log(data)
})
์์ ๊ฐ์ด ์ฌ์ฉ
์ถ์ฒ: https://gngsn.tistory.com/19 [pageseo]
์ฝ๋ฐฑ ํจ์ (Callback function)
์๋ฐ์คํฌ๋ฆฝํธ์์ ํจ์๋ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ์ ์๋๋ฐ, ๋ค๋ฅธ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ๋๊ฒจ์ค ํจ์๋ฅผ ์ฝ๋ฐฑ ํจ์๋ผ๊ณ ๋ถ๋ฅธ๋ค. ์ด๋ ํ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ ํจ์๋ก ๋ค์ ํธ์ถ๋๊ธฐ ๋๋ฌธ์ callback์ด๋ผ๋ ํํ์ ์ด๋ค.
let numbers = [1, 2, 3, 4, 5, 6, 7];
function isOddNumber(num) {
return num % 2; // number%2 === 1 (true), number%2 === 0 (false)
}
'Javascript > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฐ์คํฌ๋ฆฝํธ] ์ฑ๊ธ์ค๋ ๋, ๋น๋๊ธฐ์ฒ๋ฆฌ, ์ฝ๋ฐฑํจ์ (0) | 2022.05.29 |
---|---|
[์๋ฐ์คํฌ๋ฆฝํธ] ๋งค๊ฐ๋ณ์, ์ธ์ ์ฉ์ด ์ฐจ์ด (0) | 2022.05.27 |
[์๋ฐ์คํฌ๋ฆฝํธ] crazy this, ํท๊ฐ๋ฆฌ๋ ์๋ฐ์คํฌ๋ฆฝํธ this ์์ ์ด์ ๋ฆฌ (0) | 2022.05.22 |
[์๋ฐ์คํฌ๋ฆฝํธ] export default (0) | 2022.05.17 |
[์๋ฐ์คํฌ๋ฆฝํธ] ๋ฐ๋๋ผ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐฌ์ฑ (0) | 2022.05.10 |