function add(x, y) {
return x + y;
}
function add(x, y, z) {
return x + y + z;
}
console.log(add(1, 2)); // NaN
console.log(add(1, 2, 3)); // 6
// μμ1
let a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest); // [30, 40, 50]
// μμ2
// μ΄λ¦κ³Ό μ±μ μμλ‘ κ°μ§ λ°°μ΄
let arr = [1000, ["Bori", "Lee"]];
// ꡬ쑰 λΆν΄ ν λΉμ μ΄μ©ν΄
// firstNameμ arr[0]μ
// surnameμ arr[1]μ ν λΉνμμ΅λλ€.
let [κ³μ’μκ³ , [firstName, surname]] = arr;
console.log(κ³μ’μκ³ ); // 1000
console.log(firstName); // Bori
console.log(surname); // Lee
try...catch
try...catch
λ¬Έλ²μ μ¬μ©νλ©΄ μ€ν¬λ¦½νΈκ° μ€λ¨λλ κ²μ λ°©μ§νκ³ , μλ¬λ μ‘λλ€. try {...}
μμ μ½λ μ€νtry
μμ λ§μ§λ§ μ€κΉμ§ μ€νλκ³ catch
λΈλ‘μ 건λλ΄λ€.try
μ μ½λμ μ€νμ΄ μ€λ¨catch(error)
λΈλ‘μΌλ‘ μ μ΄ νλ¦μ΄ λμ΄κ°error
(λ³μλͺ
λ³κ²½ κ°λ₯)λ μλ¬μ λν μ€λͺ
μ΄ λ΄κΈ΄ μλ¬ κ°μ²΄λ₯Ό ν¬ν¨try {
// μ½λ
} catch (error) { // <= μλ¬ κ°μ²΄, error λμ λ€λ₯Έ μ΄λ¦ μ¬μ© κ°λ₯, (error) μμ΄λ μ¬μ© κ°λ₯
// μλ¬ νΈλ€λ§
}
// try...catch...finally
try {
... μ½λλ₯Ό μ€ν ...
} catch(e) {
... μλ¬ νΈλ€λ§ ...
} finally {
... νμ μ€ν ... // μλ¬ λ°μκ³Ό μκ΄μμ΄ λ°λμ ν λ² μ€ν
}
throw
μ°μ°μthrow
μ°μ°μλ μ¬μ©μ μ μ μμΈ(μλ¬)λ₯Ό λ°μ(throw)νλ€.throw
μ΄νμ λͺ
λ Ήλ¬Έμ μ€νλμ§ μλλ€.)catch
λΈλ‘μΌλ‘ μ μ΄ νλ¦μ΄ λμ΄κ°catch
λΈλ‘μ΄ μλ€λ©΄ νλ‘κ·Έλ¨ μ’
λ£function getRectArea(width, height) {
if (isNaN(width) || isNaN(height)) {
throw 'Parameter is not a number!'
}
}
try {
getRectArea(3, 'A'); // μ«μκ° μλ κ°μ΄ λ€μ΄κ°
} catch (e) {
console.error(e); // 'Parameter is not a number!'
}
[μΆμ²: Poiema Web - λκΈ°μ μ²λ¦¬ λͺ¨λΈκ³Ό λΉλκΈ°μ μ²λ¦¬ λͺ¨λΈ]
[μΆμ²: Poiema Web - λκΈ°μ μ²λ¦¬ λͺ¨λΈ(Synchronous processing model)]
[μΆμ²: Poiema Web - λΉλκΈ°μ μ²λ¦¬ λͺ¨λΈ(Asynchronous processing model)]
ν¨μμ 맀κ°λ³μλ₯Ό ν΅ν΄ λ€λ₯Έ ν¨μμ λ΄λΆλ‘ μ λ¬λλ ν¨μ
step1(function(value1) {
step2(value1, function(value2) {
step3(value2, function(value3) {
step4(value3, function(value4) {
step5(value4, function(value5) {
// value5λ₯Ό μ¬μ©νλ μ²λ¦¬
});
});
});
});
});
μ°Έκ³
- λͺ¨λ μλ°μ€ν¬λ¦½νΈ Deep Dive - μ΄μ λͺ¨ μ
- ꡬ쑰 λΆν΄ ν λΉ
- 'try..catch'μ μλ¬ νΈλ€λ§
- throw - MDN
μ μ½μμ΅λλΉ~!