π‘ κΈ°μ‘΄ν¨μ vs νμ΄ν ν¨μ
κΈ°μ‘΄ μλ°μ€ν¬λ¦½νΈ ν¨μ
// ν¨μννμ
let test = function(){console.log("μλ
")}; // μλ
// νμ΄ν ν¨μ
let test = () => console.log("μλ
"); // μλ
νμ΄ν ν¨μ
νμ΄ν ν¨μλ νμ μ΅λͺ ν¨μμ΄λ€.
// 맀κ°λ³μ X
let test = () => console.log('μλ
');
console.log(test);
test();
// () => console.log('μλ
')
// μλ
// 맀κ°λ³μ 1κ°
let test = x => x;
test(1);
// 1
// 맀κ°λ³μ 2κ°
let test =(a,b) => {return a+b};
test(1,2)
// 3
// returnμ΄ μλ κ²½μ°
let test = (a,b) => {a+b};
test(1,2);
// undefined
// μ¬λ¬μ€μ μμ±ν΄μΌνλ κ²½μ°
let test = (a,b) => {
let c = 3;
return a+b+c;
}
test(1,2);
// 6
{}λ₯Ό μ¬μ©νλ κ²½μ° returnμ κΌ μ¬μ©ν΄μΌνλ€.
{}λ μ½λλ₯Ό μ¬λ¬μ€ μμ±ν΄μΌν λ μ¬μ©νλ€.
π‘ κΈ°λ³Έκ° λ§€κ°λ³μ (default function paramerter)
κΈ°μ‘΄ μλ°μ€ν¬λ¦½νΈ
function multi(a,b)
{
b = (typeof b != 'undefined') ? b : 1;
return a * b
}
console.log(multi(5,2)); // 10
console.log(multi(5)); // 5
ES6
// ES2015(ES6)
function multi(a,b=1)
{
return a * b;
}
console.log(multi(5,2)); // 10
console.log(multi(5)); // 5
- κΈ°μ‘΄μλ λν΄νΈλ₯Ό μ§μ ν μ μμ΄ μ½λμμμ μ€μ μ ν΄μΌ νμ§λ§ ES6μμλ λ°λ‘ μ€μ ν μ μκ² λμλ€.
++ this λΆλΆ μΆκ°νκΈ°
μ°Έκ³
velog.io/@ki_blank/JavaScript-%ED%99%94%EC%82%B4%ED%91%9C-%ED%95%A8%EC%88%98Arrow-function
ko.javascript.info/arrow-functions-basics