๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ŒDevelopment/JavaScript

[JS] Arrow Function์€ ๋ฌด์—‡์ธ๊ฐ€? ํ•จ์ˆ˜์˜ ์„ ์–ธ๊ณผ ํ‘œํ˜„

by geumsong 2023. 3. 13.

*  Function
-  ๊ธฐ๋ณธ์ ์ธ ๋นŒ๋”ฉ๋ธ”๋Ÿญ
-  subprogram์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋ฉฐ ์—ฌ๋Ÿฌ๋ฒˆ ์žฌ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
 ํ•œ๊ฐ€์ง€์˜ task(์ž‘์—…)๋‚˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์“ฐ์ธ๋‹ค.

 

 


1. Function declaration (ํ•จ์ˆ˜ ์„ ์–ธ)

-  function ํ‚ค์›Œ๋“œ ์ด์šฉ, ์ด๋ฆ„(name)์ง€์ •, (  )์— parameter(์ธ์ž) ๋‚˜์—ด, {  } body ๋กœ์ง ์ž‘์„ฑ, returnํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
-  ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋Š” ํ•œ ๊ฐ€์ง€์˜ ์ผ๋งŒ ํ•œ๋‹ค.
-  ํ•จ์ˆ˜์ด๋ฆ„ ์ž‘์„ฑ์‹œ, ๋™์‚ฌํ˜•ํƒœ๋กœ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.
ex)
createCardAndPoint ๋ผ๋Š” ํ•จ์ˆ˜ ์ด๋ฆ„์ด ์žˆ๋‹ค๊ณ  ๋ณด์ž.
์šฐ๋ฆฐ ์ด๊ฒƒ์„ creteCard ํ˜น์€ createPoint๋กœ ๋‚˜๋ˆ ์•ผ ํ• ์ง€ ๋ง์ง€์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด์•ผํ•œ๋‹ค.
-  ํ•จ์ˆ˜๋Š” object์ด๋‹ค.


ex)
function printHello() {
console.log('Hello');
}    --->ํ•จ์ˆ˜ ์ง€์ •
printHello();  --->ํ•จ์ˆ˜ ํ˜ธ์ถœ

 

 

-  logํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๋ฉด ์›ํ•˜๋Š” ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ ๊ฐ€๋Šฅ.

ex)

log('Hello@');


-  JS์—์„œ๋Š” type์ด ์—†์–ด์„œ ์ˆซ์ž์ธ์ง€ string์„ ์ „๋‹ฌํ•ด์•ผํ•˜๋Š”์ง€ ๋ช…ํ™•X. (๋งŒ๋“ ์‚ฌ๋žŒ์ด ๋Œ€์ถฉ๋งŒ๋“ค์–ด์„œ)
๊ทธ๋ž˜์„œ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ์ด ์ˆซ์ž๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Œ.
ex)
log(1234);


-  ์ˆซ์ž๊ฐ€ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋ผ์„œ ๋กœ๊ทธ๊ฐ€ ์ถœ๋ ฅ๋˜๊ธฐ๋•Œ๋ฌธ์— ์ƒ๊ด€์—†์ง€๋งŒ ๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ ํƒ€์ž…์ด ์ค‘์š”ํ•œ ๊ฒฝ์šฐ JS๋Š” ๋‚œํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

 



*  TS(TypeScript)




: ๊ทœ๋ชจ ์žˆ๋Š” ํ”„๋กœ์ ํŠธ, ์ž‘์„ฑํ•œ ๊ฒƒ์„ library ํ˜•ํƒœ๋กœ api๋กœ ์ œ๊ณตํ• ๋• TS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค. (ํ•จ์ˆ˜์˜ interface๋งŒ ๋ด๋„ ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋Š”์ง€ ํ™•์‹คํ•˜๊ฒŒ ํ™•์ธํ•˜๊ณ  ์“ธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. JavaScript์—์„œ๋Š” ๋ช…์‹œX)
parameter์ด๋‚˜ type์„ ๋ช…์‹œํ•˜๋„๋ก ๋ผ์žˆ๋‹ค.



2. Parameter (๋งค๊ฐœ๋ณ€์ˆ˜)

// 2. Parameters
// premitive parameters: passed by value
// object parameters: passed by reference
function changeName(obj) {
    obj.name = 'coder';
}
const ellie = { name: 'ellie'};
changeName(ellie);
console.log(ellie);

-  premitive:  ๋ฉ”๋ชจ๋ฆฌ์— value๊ฐ€ ์ €์žฅ
-  object:  ๋ฉ”๋ชจ๋ฆฌ์—  referece๊ฐ€ ์ €์žฅ

 

-  changeName()์€ ์ „๋‹ฌ๋œ obj์˜ ์ด๋ฆ„์„ coder๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜.
ellie๋ผ๋Š” const ์ •์˜ ํ›„ ellie๋ผ๋Š” obj๋ฅผ ๋งŒ๋“ค์–ด ํ• ๋‹นํ•˜๋ฉด
๋ฉ”๋ชจ๋ฆฌ์—๋Š” obj๊ฐ€ ๋งŒ๋“ค์–ด์ง€ ref๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ๋“ค์–ด๊ฐ. ์ด ref๋Š”๋Š” ์ด obj๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ์–ด๋”˜๊ฐ€์— ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค
obj๋Š” ref๋กœ ์ „๋‹ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ์•ˆ์—์„œ obj์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ฒŒ๋˜๋ฉด
๊ทธ ๋ณ€๊ฒฝ๋œ ์‚ฌํ•ญ์ด ๊ทธ๋Œ€๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ์ ์šฉ ๋˜๊ธฐ์— ์ถ”ํ›„์— ๋ณ€๊ฒฝ๋œ ์‚ฌํ•ญ๋“ค์ด ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 


3.Default parameters

-  function์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ parameter๊ฐ’์„ ์‹ค์ˆ˜๋กœ ์•ˆ๋„ฃ๊ฑฐ๋‚˜ ํ–ˆ์„ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด parameter์˜ ๊ธฐ๋ณธ๊ฐ’(default๊ฐ’)์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
-  default parameter ์„ ์–ธ์€ ๋“ฑํ˜ธ(=)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
-  ๊ทธ๋Ÿฌ๋ฉด function ์‹คํ–‰์‹œ parameter๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์•˜์„ ๋•Œ default parameter๊ฐ€ ๋ฐœ๋™๋œ๋‹ค.
-  default parameter๋Š” ๋‹จ์ˆœํ•œ ๊ฐ’ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—ฐ์‚ฐ์ž, ํ•จ์ˆ˜ ๋“ฑ์„ ์ด์šฉํ•ด ๋‹ค์–‘ํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

:  showMessage์— ๋‘ ๊ฐ€์ง€ parameters (message, from)์ด ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด message๊ฐ€ ๋ˆ„๊ตฌ๋กœ๋ถ€ํ„ฐ ์™”๋Š”์ง€(from) ์ถœ๋ ฅ.
:  (๋งจ ์•„๋ž˜)  showMessage ํ˜ธ์ถœํ•  ๋•Œ ('Hi!')๋ผ๋Š” ๋ฉ”์„ธ์ง€๋งŒ ์ „๋‹ฌ๋˜๋Š” ๊ฒƒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”์‹œ์ง€๋Š” ์ถœ๋ ฅ๋˜์ง€๋งŒ from์ด ์ •์˜๋˜์ง€ ์•Š์•„undefined๋กœ ๋‚˜์˜จ๋‹ค.

===> Hi ๋ผ๋Š” ํ•˜๋‚˜์˜ ์ธ์ž๋งŒ ์ „๋‹ฌ๋˜์—ˆ๊ณ , 2๋ฒˆ์žฌ ์ธ์ž๋Š” from์— ๋Œ€ํ•ด์„œ ๊ทธ ์–ด๋–ค ๊ฒƒ๋„ ์ „๋‹ฌ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—

 


-  parameter๊ฐ’์ด ์ •์˜๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ, 

if (from === undefined) {
from = 'unknown';
}

(๊ทธ ์ค‘ ์ด๋ถ€๋ถ„)
-  ์˜ˆ์ „์—” from์ด undefined์ด๋ฉด unknown์ด๋ผ๋Š” ๊ธฐ๋ณธ์ ์ธ string์„ ์ถ”๊ฐ€ํ•˜์ž ๋ผ๊ณ  ํ•ด์„œ unknown์ด๋ผ๊ณ  ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

- ํ•˜์ง€๋งŒ ์ง€๊ธˆ์€ ์ด๋ ‡๊ฒŒ ์ ์ง€ ์•Š์•„๋„ 

function showMessage(message, from = 'unknown') {
console.log(`${message} by ${from}`);
}

์ด๋ผ๊ณ  ์ง€์ •ํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ parameter๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์„๋•Œ ์ด ๊ฐ’(unknown)์ด ๋Œ€์ฒด๋˜์–ด ์‚ฌ์šฉ๋œ๋‹ค.

 

 


4. Rest parameters (added in ES6)

: ๋ง ๊ทธ๋Œ€๋กœ rest(์—ฌ๋ถ„, ๋‚˜๋จธ์ง€) parameter๋ผ๋Š” ์˜๋ฏธ.

: . . .์„ ์‚ฌ์šฉํ•œ๋‹ค.

:๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ „๋‹ฌ

// 4. rest parameters (added in ES6)
function printAll(...args) {
    for (let i = 0; i < args.length;) i++) {
        console.log(args [i]);
    }
}
printAll('dream', 'coding', 'ellie');

-  printAll์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ 3๊ฐœ์˜ ์ธ์ž(dream, coding, ellie)๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

-  . . . args = 3๊ฐœ์˜ ๊ฐ’์ด ๋‹ด๊ฒจ์ ธ์žˆ๋Š” ๋ฐฐ์—ด.

-  for๋ฌธ ์ด์šฉํ•ด์„œ "์ฒ˜์Œ๋ถ€ํ„ฐ arg์˜ ๊ฐฏ์ˆ˜๋งŒํผ ๋Œ๋ฉด์„œ

1๋ฒˆ์งธ ์ธ์ž(dream), ๊ทธ ๋‹ค์Œ์—๋Š” 

2๋ฒˆ์งธ ์ธ์ž(coding), ๊ทธ ๋‹ค์Œ์—”

3๋ฒˆ์งธ ์ธ์ž(ellie) ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

===> for๋ฌธ์„ ํ†ตํ•ด ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•ด๋„ ๋˜๊ณ  ๊ทธ ์™ธ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ๋„ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

-<๋ฐฐ์—ด ์ถœ๋ ฅํ•˜๋Š” ๋ฒ•>

:  for์ด์šฉํ•ด๋„ ๋˜์ง€๋งŒ  for .. of๋ผ๋Š” ๋ฌธ๋ฒ•์œผ๋กœ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

for (const arg of args) {
	console.log(arg);
    }

-  args์— ์žˆ๋Š” ๊ฒŒ ํ•˜๋‚˜์”ฉ ์ง€์ •๋˜๋ฉด์„œ log(arg)๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ.

 

 

 

-  ๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” forEach๋ผ๋Š” ํ•จ์ˆ˜ํ˜• ์–ธ์–ด๋ฅผ ์ด์šฉํ•ด ์ถœ๋ ฅ๊ฐ€๋Šฅํ•˜๋‹ค. ๋” ์ž์„ธํ•œ ๊ฑด ๋ฐฐ์—ด๊ณต๋ถ€ํ•˜๊ธฐ

args.forEach((arg) => console.log(arg));

 

 


5. Local Scope (์ง€์—ญ๋ณ€์ˆ˜)

-  ์›๋ฆฌ: ๋ฐ–์—์„œ๋Š” ์•ˆ์ด ๋ณด์ด์ง€ ์•Š๊ณ  ์•ˆ์—์„œ๋งŒ ๋ฐ–์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

// 5. Local scope
let globalMessage = 'global'; // global variable
function printMessage() {
    let message = 'hello';
    console.log(message); // local variable
    console.log(globalMessage);
}
printMessage();

scope์—๋Š” 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

1. Global scope

2. Local scope 

 

 

: ๋ธ”๋Ÿญ{  } ์•ˆ์—์„œ ๋ณ€์ˆ˜ ์„ ์–ธํ•˜๋Š” ๊ฒƒ = local varialbe (์ง€์—ญ๋ณ€์ˆ˜)
๋ธ”๋Ÿญ {  }์•ˆ์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ
:  {  }๋ธ”๋Ÿญ ๋ฐ–์—์„œ ๋ฐ–์—์„œ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด ์—๋Ÿฌ๋ฐœ์ƒ.

ex)

-  console.log(message); ์ถœ๋ ฅ์‹œ ์šฐ์ธก ์ฝ˜์†”์—์„œ ์—๋Ÿฌ ๋ฐœ์ƒ.

 

 

printMessage() ๋ผ๋Š” ํ•จ์ˆ˜ ์•ˆ์— printAnother() ์ด๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋˜ ์žˆ๋‹ค.

์ž์‹์€ ๋ถ€๋ชจ์—๊ฒŒ์„œ ์ •์˜๋œ ๋ฉ”์‹œ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

-  ํ•˜์ง€๋งŒ ์ž์‹ ์•ˆ์— ์ •์˜๋œ childMessage๋ฅผ ๋ถ€๋ชจ์—๊ฒŒ์„œ ๋ณด๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚  ๊ฒƒ.

:  ์ž์‹์˜ ํ•จ์ˆ˜๊ฐ€ ๋ถ€๋ชจํ•จ์ˆ˜์— ์ •์˜๋œ ๋ณ€์ˆ˜๋“ค๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 


6. Return a value

: return:  ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ๋‚˜์„œ return์„ ์“ฐ๋ฉด ๊ทธ์ž๋ฆฌ์— return์— ํ• ๋‹นํ•œ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.

// 6. Return a value
function sum(a, b) {
    return a + b;
}
const result = sum(1, 2); //3
console.log(`sum: ${sum(1, 2)}`);

:  sum์ด๋ผ๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ 1๊ณผ 2๋ฅผ ํ•ฉํ•œ ๊ฐ’์ธ 3์ด return๋จ

-  returnํƒ€์ž…์ด ์—†๋Š” ํ•จ์ˆ˜๋“ค์€ return undefined์ด ๋“ค์–ด์žˆ๋Š” ๊ฒƒ๊ณผ ๋˜‘๊ฐ™๋‹ค.

 


7. Early return, early exit

// 7. Early return, early exit
// bad
function upgradeUser(user) {
    if (user.point > 10) {
        //long upgrade logic...
    }
}

-  upgradeUserํ•จ์ˆ˜ ์•ˆ์—์„œ user.point๊ฐ€ 10์ด์ƒ์ผ ๋•Œ ์ง„ํ–‰ํ•˜๋Š” ๋กœ์ง์ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด if ๋ธ”๋Ÿญ {  }์•ˆ์—์„œ ๋กœ์ง ์ž‘์„ฑ์‹œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ if์™€ else๋ฅผ ๋ฒˆ๊ฐˆ์•„์„œ ์“ฐ๋Š” ๊ฒƒ ๋ณด๋‹ค๋Š”

 

//good
function upgreadeUser(user) {
    if (user.point <= 10) {
        return;
    }
     // long upgrade logic...
}

 -  ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์„ ๋• ๋นจ๋ฆฌ returnํ•ด์„œ ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ์กฐ๊ฑด ๋งž์„๋•Œ๋งŒ ๊ทธ ๋‹ค์Œ์— ํ•„์š”ํ•œ logic์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค.

 

-  ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์€ ๊ฒฝ์šฐ / ๊ฐ’์ด undefined์ธ ๊ฒฝ์šฐ / ๊ฐ’์ด -1์ธ ๊ฒฝ์šฐ ๋นจ๋ฆฌ returnํ•˜๊ธฐ.

 

 

 


Function Expression (ํ•จ์ˆ˜ ํ‘œํ˜„)

// First-class function
// functions are treated like any other variable
// can be assigned as a value to variable
// can be passed as an argument to other functions.
// can be returned by another function

<๋ณต์Šต>

- first-class function์€ ๋‹ค๋ฅธ ํ•จ์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ

 ๋ณ€์ˆ˜์— ํ• ๋‹น์ด ๋˜๊ณ 

function์˜ parameter๋กœ ์ „๋‹ฌ

return๊ฐ’์œผ๋กœ๋„ return๋œ๋‹ค.

 


1. Function expression (ํ•จ์ˆ˜ํ‘œํ˜„)

// 1. Function expression
// a function declaration can be called earlier than it is defined. (hoisted)
// a function expression is created when the execution reaches it.
const print = function () {
    console.log('print');
};
print();
const printAgain = print;
printAgain();
const sumAgain = sum;
console.log(sumAgain(1, 3));

-  function() ์— ์ด๋ฆ„์ด ์—†๊ณ , function์„ ์ด์šฉํ•ด parameter์™€ { }๋ธ”๋Ÿญ ์ด์šฉํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

ํ•จ์ˆ˜์— ์ด๋ฆ„์ด ์—†๋Š” ๊ฒƒ์„ anonymous function์ด๋ผ๊ณ ํ•œ๋‹ค.

-  ํ•จ์ˆ˜์˜ ์ด๋ฆ„ ์—†์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„( console.log('print'); )๋งŒ ์ž‘์„ฑํ•ด์„œ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๊ณ 

ํ•จ์ˆ˜ ์ด๋ฆ„์„ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. = named function

ex)

const print = function print() { }

 

 

 

-  ํ•จ์ˆ˜๋ฅผ print์— ํ• ๋‹นํ•˜๋ฉด print๋ผ๋Š” ๋ณ€์ˆ˜์— ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋“ฏ (); ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ๋ฐ”๋กœ ํ”„๋ฆฐํŠธ ์ถœ๋ ฅ๋จ.

 

 

 

-๋‹ค์‹œ ๋‹ค๋ฅธ ๋ณ€์ˆ˜ (const printAgain)์— ํ• ๋‹นํ•˜๊ฒŒ ๋˜๋ฉด

printAgain์€ function์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์œผ๋ฏ€๋กœ

printAgain(); ์˜ ๊ฐ’์€ print๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

- (์•„๊นŒ ์œ„์—์„œ sum์ด๋ผ๋Š” ํ•จ์ˆ˜ ๋งŒ๋“ค์—ˆ์Œ)

์ด๊ฒƒ๋„ sumAgain์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•˜๊ฒŒ ๋˜๋ฉด ๋™์ผํ•˜๊ฒŒ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๐Ÿ“Function declaration ๊ณผ Function expression ์ฐจ์ด์ 

- function expression 

:  ํ• ๋‹น๋œ ๋‹ค์Œ ๋ถ€ํ„ฐ ํ˜ธ์ถœ ๊ฐ€๋Šฅ 

 

-  Function declaration

:  hoisting๋œ๋‹ค. (function์ด ์„ ์–ธ๋˜๊ธฐ ์ด์ „์— ํ˜ธ์ถœํ•ด๋„ ๋’ค์—์„œ sum์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ธฐ ์ „์— JS์—”์ง„์ด ์„ ์–ธ๋œ ๊ฒƒ์„ ์ œ์ผ ์œ„๋กœ ์˜ฌ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

 

 


2. Callback function using function expreession

*  Callback(์ฝœ๋ฐฑ): ์€ ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ์ธ์ˆ˜๋กœ์„œ ๋„˜๊ฒจ์ฃผ๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ(ํ•จ์ˆ˜๋กœ ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค!)๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
์ฆ‰, ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ Callback์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค!

๐Ÿ“callback function์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ?
:  ๋‹ค๋ฅธ ์ฝ”๋“œ(ํ•จ์ˆ˜)์˜ ์ธ์ˆ˜๋กœ์„œ ๋„˜๊ฒจ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธ!

-  ์‚ฌ์šฉํ•˜๋Š” ์ด์œ : ํ”„๋กœ๊ทธ๋žจ์ด ๋น„์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋์„ ๋•Œ ์‚ฌ์šฉ

// 2. Callback function using function expreession
function randomQuiz(answer, printYes, printNo) {
    if (answer === 'love you') {
        printYes();
    } else {
        printNo();
    }
}

- answer : ์ •๋‹ต

- printYes : ์ •๋‹ต์ผ ๋•Œ

- printNo : ์ •๋‹ต์ด ์•„๋‹ ๋•Œ

์ƒํ™ฉ์— ๋งž์„ ๋•Œ ์ „๋‹ฌ๋œ ํ•จ์ˆ˜(printYes, printNo) ๋ฅผ ๋ถ€๋ฅด๋Š” ๊ฒƒ = callback function

์ฆ‰, ๋‘ ๊ฐ€์ง€์˜ callback functions๊ฐ€ parameter๋กœ ์ „๋‹ฌ๋ผ์„œ answer์ด love you์ธ ๊ฒฝ์šฐ์—๋งŒ 

printYes๋ผ๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ, ์•„๋‹๊ฒฝ์šฐ printNo๋ผ๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜ ํ˜ธ์ถœํ•จ.

 

 

 

const printYes = function() {
    console.log('yes!');
};

const printNo = functino print() {
    console.log('no!');
};
randomQuiz('wrong', printYes, printNo);
randomQuiz('love you', printYes, printNo);

- printYes๋ผ๋Š” ๋ณ€์ˆ˜์— yes๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜(function())๋ฅผ ํ• ๋‹นํ•˜๊ณ 

printNo๋ผ๋Š” ๋ณ€์ˆ˜์—๋Š” no๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜((function print())๊ฐ€ ์˜จ๋‹ค.

- randomQuizํ˜ธ์ถœํ•  ๋•Œ ์ฐจ๋ก€๋Œ€๋กœ ์ •๋‹ต, yes์ฝœ๋ฐฑํ•จ์ˆ˜, no์ฝœ๋ฐฑํ•จ์ˆ˜ ์ถœ๋ ฅ.

 

 

-  ์ด๋ฆ„ ์—†๋Š” ํ•จ์ˆ˜ = anonymous function

// anonymous function
const printYes = function() {
    console.log('yes!');
};

 

- named function

// named function
// better debuffing in debugger's stack traces
// recursions
const printNo = function print() {
    console.log('no!');
};

:  ํ•จ์ˆ˜์˜ ์ด๋ฆ„: print

 

๐Ÿ“์ด๋ฆ„ ์“ฐ๋Š” ์ด์œ 

:  ๋””๋ฒ„๊น…ํ• ๋•Œ ์“ฐ์ž„

:  ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์“ฐ์ž„( =recursions),

:  ๋ฐ˜๋ณต๋˜๋Š” ํ‰๊ท ๊ฐ’ ๊ณ„์‚ฐ ๋“ฑ ์“ฐ์ž„์ƒˆ๊ฐ€ ๋‹ค์–‘ํ•˜๋‹ค.

===>์•Œ์•„๋งŒ๋‘๊ธฐ

 

 

-ํ•จ์ˆ˜๋ฅผ ๋ฌดํ•œ๋Œ€๋กœ ํ˜ธ์ถœํ•˜๋ฉด call stack์ด ๊ฝ‰ ์ฐผ๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

===>์•Œ์•„๋งŒ๋‘๊ธฐ

 

 


Arrow Function

: ํ•จ์ˆ˜๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.
: ํ•ญ์ƒ ์ด๋ฆ„์ด ์—†๋Š” anonymous function.

// Arrow function
// always anonymous
const simplePrint = function () {
    console.log('simplePrint!');
};

- ๋ฌธ์ œ์ : function () { } ๊ด„ํ˜ธ ์ค‘๊ด„ํ˜ธ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์จ์ค˜์•ผํ•œ๋‹ค.

 

 

const simplePrint = () => console.log('simplePrint!');

- ์ € ์œ„์— ์ฝ”๋“œ๋ฅผ ์ด๋ ‡๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ์Œ.

 

 

๋˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž

// 1๋ฒˆ
const add = (a, b) => a + b;

//2๋ฒˆ
const add = function (a, b) {
    return a + b;
}

1๋ฒˆ๊ณผ 2๋ฒˆ ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ๋” ๊ฐ„๊ฒฐํ•ด ๋ณด์ด๋Š”๊ฐ€?

1๋ฒˆ์ด๋‹ค.

 

 

๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ๋•Œ ๋ธ”๋Ÿญ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด, ์ด๋Ÿฐ์‹์œผ๋กœ ๋ธ”๋Ÿญ์„ ๋„ฃ์„ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

const simpleMultiply = (a, b) => {
    // do something more
    return a * b;
}

- ๋ธ”๋Ÿญ ์“ฐ๋ฉด returnํ‚ค์›Œ๋“œ ์จ์„œ return ํ•ด์•ผํ•œ๋‹ค.

 

 


 

IIFE

: ํ•จ์ˆ˜ ์„ ์–ธํ•˜๋ฉด ๋‚˜์ค‘์— ํ˜ธ์ถœ์„ ํ•ด์ค˜์•ผํ•œ๋‹ค. ์„ ์–ธ๊ณผ ๋™์‹œ์— ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•.

:  ์ตœ๊ทผ์—๋Š” ์ž˜ ์“ฐ์ด์ง€๋Š” ์•Š์Œ.

// IIFE: Immediately Invoked Function Expression
function hello(){
    console.log('IIFE');
}

 

728x90

์ตœ๊ทผ๋Œ“๊ธ€

์ตœ๊ทผ๊ธ€

skin by ยฉ 2024 ttutta