Chapter 03: ํ•จ์ˆ˜ ๊ธฐ๋ณธ๊ฐ’ ์ธ์ˆ˜ (Default function arguments)

๐Ÿ’ก ๋„์„œ ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•ต์‹ฌ ๊ฐ€์ด๋“œ๋ฅผ ์ฝ๊ณ  ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

3.1 ํ•จ์ˆ˜ ์ธ์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’(ES6 ์ด์ „)


ES6 ์ด์ „์—๋Š” ํ•จ์ˆ˜ ์ธ์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š์•˜๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋กœ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

function getLocation(city,country,continent){
  if(typeof country === 'undefined'){
    country = 'Italy'
  }
  if(typeof continent === 'undefined'){
    continent = 'Europe'
  }
  console.log(continent,country,city)
}

getLocation('Milan')
// Europe Italy Milan

getLocation('Paris','France')
// Europe France Paris

์ธ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ์ฝ”๋“œ๋Š” ๋”์šฑ ๋ณต์žกํ•ด์ง„๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ parameter์— undefined ๋ฅผ ์ผ์ผํžˆ ์ „๋‹ฌํ•ด์•ผ๋งŒ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

function getLocation(continent,country,city){
  if(typeof country === 'undefined'){
    country = 'Italy'
  }
  if(typeof continent === 'undefined'){
    continent = 'Europe'
  }
  console.log(continent,country,city)
}

getLocation(undefined, undefined,'Milan')
// Europe Italy Milan

getLocation(undefined,'Paris','France')
// Europe Paris France

3.2 ํ•จ์ˆ˜ ๊ธฐ๋ณธ๊ฐ’ ์ธ์ˆ˜ (ES6 ์ดํ›„)


ES6 ์ดํ›„ ํ•จ์ˆ˜ ๊ธฐ๋ณธ๊ฐ’ ์ธ์ˆ˜๋ฅผ ๋งค์šฐ ์‰ฝ๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

function calculatePrice(total, tax = 0.1, tip = 0.05){
// When no value is given for tax or tip, the default 0.1 and 0.05 will be used
return total + (total * tax) + (total * tip);
}

Use Destructuring Assignment

๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง(๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น)์„ ์ด์šฉํ•ด ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

// Use Destructuring Assignment (ํ•จ์ˆ˜์˜ parameter๋ฅผ ๊ฐ์ฒด๋กœ ์„ค์ •)
function calculatePrice({
  total = 0,
  tax = 0.1,
  tip = 0.05} = {}){
  return total + (total * tax) + (total * tip);
}

const bill = calculatePrice({ tip: 0.15, total:150 });
// 187.5

๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์„ ์ด์šฉํ•ด ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ์ฒด๋กœ ์„ค์ •ํ•˜๋ฉด ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์ „๋‹ฌํ•˜๋“  ์ƒ๊ด€์—†์ด ๊ทธ ์ธ์ˆ˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋œ๋‹ค. ์ด๋•Œ ์ธ์ˆ˜์˜ ๋งˆ์ง€๋ง‰์— ={} ๊ธฐํ˜ธ๋ฅผ ๋น ๋œจ๋ฆฌ์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ์ด ๊ธฐํ˜ธ๊ฐ€ ์—†์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ={} ๊ธฐํ˜ธ์˜ ์œ ๋ฌด์— ๋”ฐ๋ฅธ ๊ฒฐ๊ณผ ์ฐจ์ด๋ฅผ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด๋ณด์ž.

={}๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

function calculatePrice({
  total = 0,
  tax = 0.1,
  tip = 0.05} = {}){
  return total + (total * tax) + (total * tip);
}

calculatePrice({});
// 0
calculatePrice();
// 0
calculatePrice(undefined)
// 0

={}๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ

function calculatePrice({
  total = 0,
  tax = 0.1,
  tip = 0.05}){
  return total + (total * tax) + (total * tip);
}
calculatePrice({});
// cannot read property `total` of 'undefined' or 'null'.
calculatePrice();
// cannot read property `total` of 'undefined' or 'null'.
calculatePrice(undefined)
// cannot read property `total` of 'undefined' or 'null'.

3.3 QUIZ


Q 3.1. ๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

๋‹ค์Œ์ฝ”๋“œ์—์„œ arg1๊ณผ arg2๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ๋Š” tax๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ๋‘ ๋ฒˆ์งธ๋Š” tip์˜ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๊ฒŒ ๋งŒ๋“ค์–ด๋ณด์ž.

tax์—๋Š” ๊ธฐ๋ณธ๊ฐ’ 0.1์„ ์ง€์ •ํ•˜๊ณ  tip์—๋Š” ๊ธฐ๋ณธ๊ฐ’ 0.05๋ฅผ ์ง€์ •ํ•˜์ž.

function calculatePrice(total, arg1, arg2) {
    return total + (total * tax) + (total * tip);
}
calculatePrice(10);
// ์˜ˆ์ƒ ๊ฒฐ๊ณผ: 11.5
  • ๋‹ต
  • function calculatePrice(total, tax = 0.1, tip = 0.05) { return total + (total * tax) + (total * tip); }

Q 3.2. ๋‹ค์Œ ์ฝ”๋“œ์˜ ์˜ฌ๋ฐ”๋ฅธ ์ถœ๋ ฅ์€?

var b = 3;
function multiply(a, b = 2) {
    return a * b;
}
console.log(multiply(5));
  • ๋‹ต
  • 10