π‘ ν¨μμ 맀κ°λ³μ κ°―μλ₯Ό λμ΄κ°λ μΈμ
- μ€λ₯λ₯Ό μΌμΌν€μ§ μκ³ λ¬΄μλ¨
function add(a, b) {
return a + b;
}
β
console.log(
add(1, 3),
add(1, 3, 5),
add(1, 3, 5, 7)
);
I. κΈ°λ³Έκ° λ§€κ°λ³μ default parameter
π MDN λ¬Έμ 보기
function add(a = 2, b = 4) {
console.log(`${a} + ${b}`);
return a + b;
}
β
console.log(
add(),
add(1),
add(1, 3)
);
II. arguments - ν¨μ λ΄μμ μ¬μ©κ°λ₯ν μ§μ λ³μ
π MDN λ¬Έμ 보기
- λ°°μ΄μ ννλ₯Ό ν κ°μ²΄ - λ°°μ΄μ μλμ§λ§ μ¬μ€μ λ°°μ΄μ²λΌ λμ (λ°°μ΄λ μ¬μ€ κ°μ²΄μ§λ§...)
- ν¨μ νΈμΆ μ μ λ¬λ λͺ¨λ μΈμλ€μ λ°°μ΄ ννλ‘ κ°μ§
function add(a, b) {
console.log('1.', arguments);
console.log('2.', arguments[0]);
console.log('3.', typeof arguments);
return a + b;
}
β
console.log(
'4.', add(1, 3, 5, 7)
);
function add(a, b) {
for (const arg of arguments) {
console.log(arg);
}
return a + b;
}
β
console.log(
add(1, 3, 5, 7)
);
- for ... ofκ° κ°λ₯ν μ΄μ : iterableμ΄κΈ° λλ¬Έ μ΄ν λ€λ£Έ
- β οΈ νμ΄ν ν¨μμμλ arguments μ¬μ© λΆκ°! νμ΄ν ν¨μλ‘ λ°κΎΈμ΄ μ€νν΄ λ³Ό κ²
function getAverage() {
let result = 0;
for (const num of arguments) {
result += num;
}
return result / arguments.length;
}
β
console.log(
getAverage(1, 4, 7),
getAverage(24, 31, 52, 80)
);
// π‘ νμ
μ μμ ν λ²μ
function getAverage() {
let result = 0, count = 0;
for (const num of arguments) {
if (typeof num !== 'number') continue;
result += num;
count++;
}
return result / count;
}
β
console.log(
getAverage(2, 'κ°', 8, true, 5)
);
// β»οΈ μλ‘κ³ μΉ¨ ν μ€ν
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const mul = (a, b) => a * b;
const div = (a, b) => a / b;
β
function combineArms () {
return (x, y) => {
let result = x;
for (const arm of arguments) {
if (typeof arm !== 'function') continue;
result = arm(result, y);
}
return result;
}
}
β
const add_mul = combineArms(add, mul, 1, true);
const add_mul_sub = combineArms(add, mul, sub);
const add_mul_sub_div = combineArms(add, mul, sub, div);
β
// π‘ μ΅λͺ
ν¨μ μ¬μ©λ¨
const add_mul_sub_div_pow
= combineArms(add, mul, sub, div, (x, y) => x ** y);
console.log(
add_mul(8, 3),
add_mul_sub(8, 3),
add_mul_sub_div(8, 3),
add_mul_sub_div_pow(8, 3)
);
III. ...λ³μκ·Έλ£Ήλͺ
- λλ¨Έμ§ λ³μ rest parameters
π MDN λ¬Έμ 보기
- νΉμ 맀κ°λ³μλ€ λ€μ μ ν΄μ§μ§ μμ μμ 맀κ°λ³μλ€μ λ°μ λ
- λ§μ§λ§ μΈμλ‘λ§ μ¬μ© κ°λ₯
- argumentsμλ λ¬λ¦¬ μ€μ λ°°μ΄μ
console.log(
'3.',
classIntro(3, 'κΉλ―Όμ§', 'μν¬', 'μ² μ', '보λΌ')
); // νΈμ΄μ€ν
β
function classIntro (classNo, teacher, ...children) {
console.log('1.', children);
console.log('2.', arguments);
β
let childrenStr = '';
for (const child of children) {
if (childrenStr) childrenStr += ', ';
childrenStr += child;
}
return `${classNo}λ°μ μ μλμ ${teacher}, `
+ `νμλ€μ ${childrenStr}μ
λλ€.`
}
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const mul = (a, b) => a * b;
const div = (a, b) => a / b;
β
function doMultiArms (x, y, ...arms) {
let result = x;
for (const arm of arms) {
if (typeof arm !== 'function') continue;
result = arm(result, y);
}
return result;
}
β
console.log(
doMultiArms(8, 3, add, mul, 1, true),
doMultiArms(8, 3, add, mul, sub),
doMultiArms(8, 3, add, mul, sub, div),
doMultiArms(8, 3, add, mul, sub, div, (x, y) => x ** y)
);
I. μ€μ²©λ ν¨μ
function outer () {
const name = 'λ°κΉ₯μͺ½'
console.log(name, 'ν¨μ');
β
function inner () {
const name = 'μμͺ½'
console.log(name, 'ν¨μ');
}
inner();
}
β
outer();
function addMulSub (x, y) {
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const mul = (a, b) => a * b;
β
return sub(mul(add(x, y), y), y);
}
β
console.log(addMulSub(8, 3));
II. μ¬κ· ν¨μ recursive function
function upto5 (x) {
console.log(x);
if (x < 5) {
upto5(x + 1);
} else {
console.log('- - -');
};
}
β
upto5(1);
upto5(3);
upto5(7);
- μ€νμ΄ λμΉλ©΄ stack overflow - μ€λ₯ λ°μ
π‘ ν©ν λ¦¬μΌ factorial μ¬κ·ν¨μ
function fact(x) {
return x === 0 ? 1 : x * fact(x - 1);
}
λλ
function fact(a) {
return !a ? 1 : a * fact(a-1) ;
}
β
console.log(
fact(1),
fact(2),
fact(3),
fact(4)
)
III. μ¦μ μ€ν ν¨μ IIFE
π MDN λ¬Έμ 보기
- Immideately Invoked Function Expression
- μ€λλ μλ μ μ¬μ©λμ§ μμ - κ³Όκ±° μ½λ λΆμμ μν΄...
(function () {
console.log('IIFE');
})();
π‘ 무μμ μ¬μ©λμλκ°?
const initialMessage = (function () {
// β οΈ varλ₯Ό μ¬μ©ν¨μ μ£Όλͺ©
var month = 8;
var day = 15;
var temps = [28, 27, 27, 30, 32, 32, 30, 28];
var avgTemp = 0;
for (const temp of temps) {
avgTemp += temp
}
avgTemp /= temps.length;
β
return `${month}μ ${day}μΌ νκ· κΈ°μ¨μ μμ¨ ${avgTemp}λμ
λλ€.`;
})();
β
console.log(initialMessage);
console.log(month);
- λ± ν λ²λ§ μ¬μ©λ ν¨μμ
- μ μ λ³μλ€μ μ¬μ©νμ§ μκ³ , 볡μ‘ν κΈ°λ₯μ μΌνμ±μΌλ‘ μ€νν λ
- λ€λ₯Έ μ½λλ€κ³Όμ λ³μλͺ
μ΄λ μμλͺ
μΆ©λμ λ§κΈ° μν¨ (νΉν λ§μ μ½λλ€μ΄ μ¬μ©λ λ)
- μ€λλ μλ λΈλ‘κ³Ό μ΄ν λ°°μΈ λͺ¨λμ μ¬μ©μΌλ‘ λ체
- μ΄μ μ varλ λΈλ‘ μΈμμ μ¬μ©λ μ μμμ(βΌοΈ)
let initialMessage;
β
{
const month = 8;
const day = 15;
const temps = [28, 27, 27, 30, 32, 32, 30, 28];
let avgTemp = 0;
for (const temp of temps) {
avgTemp += temp
}
avgTemp /= temps.length;
β
initialMessage = `${month}μ ${day}μΌ νκ· κΈ°μ¨μ μμ¨ ${avgTemp}λμ
λλ€.`;
};
β
console.log(initialMessage);
console.log(month); // μλ‘κ³ μΉ¨ ν constλ₯Ό varλ‘ λ°κΎΈκ³ μ€νν΄λ³Ό κ²
IV. βοΈ λΆλ³μ± immutability
let x = 1;
let y = {
name: 'νκΈΈλ',
age: 15
}
let z = [1, 2, 3];
β
function changeValue (a, b, c) {
a++;
b.name = 'μ μ°μΉ';
b.age++;
c[0]++;
β
console.log(a, b, c);
}
β
changeValue(x, y, z);
μμ νμ
: μΈμλ‘ λ€μ΄κ° ν¨μ λ΄μμμ λ³κ²½μ μν₯ λ°μ§ μμ
- μ€μ κ°μ΄ μλλΌ λ³΅μ¬λ κ°μ΄ λ€μ΄κ°κΈ° λλ¬Έ
μ°Έμ‘° νμ
: μΈμλ‘ λ€μ΄κ° ν¨μ λ΄μμ μμκ° λ³νλ©΄ μ€μ λ‘λ λ³ν¨
- 볡μ¬λ κ°λ κ°μ κ°μ²΄λ λ°°μ΄μ κ°λ¦¬ν€κΈ° λλ¬Έ
βοΈ ν¨μμ μ£Όμ΄μ§ μΈμλ₯Ό λ³κ²½νλ κ²μ μ’μ§ μμ
- β οΈ μΈλΆμ νκ²½μ λ³κ²½νλ ν¨μλ μν!
- μ΄μμ μΈ ν¨μ: λ°μ κ°λ€λ§ μ²λ¦¬νμ¬ μ κ°μ λ°ν