λ°μν
πͺ 볡ꡬ κ°λ₯ν μ€λ₯
- μ¬μ©μκ° μλͺ»λ μμμΌλ‘ μ
λ ₯νλ κ²½μ°, μ¬μ©μμκ² μλ΄λ₯Ό ν΅ν΄ 볡ꡬ κ°λ₯
- μ νλ²νΈ μμμ μ§ν€μ§ μλ κ²½μ°
- λ€νΈμν¬ μλ¬λλ κ²½μ° μ¬μ©μμκ² μ μ ν μ΄μ©κ°λ₯νλλ‘ μλ΄ κ°λ₯
πΏ 볡ꡬν μ μλ μ€λ₯
- μμ§λμ΄μ μ€μλ‘ μΈν κ²
- μ½λμ ν¨κ» μΆκ°λμ΄μΌ νλ 리μμ€
- νλ‘μ νΈ νκ²½ λ³μ , κΈ°λ³Έ μ€μ κ°λ€
- DBμ 보 λ±λ±
- νλ‘μ νΈ νκ²½ λ³μ , κΈ°λ³Έ μ€μ κ°λ€
- μ΄λ€ μ½λκ° λ€λ₯Έμ½λλ₯Ό μλͺ» νΈμΆνλ κ²½μ°
- μΈμλ₯Ό μλͺ» νΈμΆνλ€λμ§
π½ μ μνκ² μ€ν¨νκΈ°/μλνκ² μ€ν¨νκΈ°
κ°λ μλ―Έ
| μ μνκ² μ€ν¨ | κ°λ₯ν λ¬Έμ κ° λ°μν μμ λΆν° μ€λ₯λ₯Ό λνλ΄λ κ² |
| μλνκ² μ€ν¨ | μ€ν¨λ₯Ό μ¨κΈ°μ§ μκ³ μ리λκ² |
μ μνκ² μ€ν¨νκΈ° μμ
- μλͺ»λ μΈμλ₯Ό λ£κ±°λ ν κ²½μ°
// λ¬Έμ κ° μκΈ΄μͺ½μμ λΆν° μ€λ₯λ₯Ό λνλΈλ€
function prepareUrl(url) {
if (!url) throw new Error("URLμ΄ μ‘΄μ¬νμ§ μμ΅λλ€");
console.log("setting url");
}
function playMusic() {
prepareUrl();
}
playMusic();
μλνκ² μ€ν¨νκΈ° μμ
// throwλ₯Ό ν΅ν΄ ν¨μλ₯Ό νΈμΆνλκ³³μμ μΈμ§νλλ‘ νλ€
function prepareUrl(url) {
if (!url) throw new Error("URLμ΄ μ‘΄μ¬νμ§ μμ΅λλ€");
console.log("setting url");
}
function playMusic() {
prepareUrl();
}
playMusic();
πΌ μ€λ₯ μ λ¬ λ°©λ²
λͺ μμ λ°©λ²
μ½λμ μ€λ₯κ° μμμ μΈμ§ν μ λ°μ νλκ²
- κ²μ¬μμΈ: JSμμλ κ°μ λ‘ κ²μ¬μμΈ μ²λ¦¬νλκ²μ΄ μ‘΄μ¬νμ§ μλλ€.
- λκ°μ μ΄μ©ν μ€λ₯ μ λ¬: λκ°μ λ°ννλ λ°©μμ΄λ©°, μ€λ₯κ° λ°μν μ΄μ λν μ 보λ₯Ό μ 곡νμ§ μλλ€
/**
* @param {Object} value - λμ΄λ₯Ό κ³μ°ν κ°μ²΄
* @param {number} value.width - μ§μ¬κ°νμ λλΉ
* @param {number} value.height - μ§μ¬κ°νμ λμ΄
* @returns {undefined|number}
*/
function getArea(value) {
// μ€λ₯κ° λ°μνλ©΄ κ°μ΄ μλκ±΄μ§ μ€λͺ
μ΄ μλ€.
// μ£Όμμ λ€λ λ°©μλ μκ² μ§λ§, μ ννμ§ μμ κ°λ₯μ±μ΄ λλ€
if (!value) return;
return value.width * value.height;
}
// λκ°μ λν μ€λ₯ μ²λ¦¬
function displayArea() {
const result = getArea({
width: 2,
height: 3,
});
if (!result) {
console.error("displayArea error result is undefined");
} else {
console.log("success displayArea : " + result);
}
}
displayArea();
- 리μ νΈ λ°ν μ ν: λκ°λ°νμ μ΄λ€ μ€λ₯μΈμ§ κ°μ§κ° λΆκ°λ₯νλ€λ λ¨μ μ΄ μλ€. 그리νμ¬ Resultμ νμ μ¬μ©νλ©΄ μ€λ₯μ λν μ 보λ κ°μ΄ μ λ¬μ΄ κ°λ₯νλ€. λ€λ§ μλ¬κ° μ‘΄μ¬νλμ§(hasError) νμΈνκ³ , μμΌλ©΄ getValueλ₯Ό ν΅ν΄ νμΈν΄μΌνλ€. μ΄κ²μ μΈμ§λͺ»νλ©΄ , μμμ μ€λ₯κ° μ‘΄μ¬νλ€
class Result {
#value;
#error;
constructor(value, error) {
this.#value = value;
this.#error = error;
}
static ofValue(value) {
return new Result(value, null);
}
static ofError(error) {
return new Result(null, error);
}
hasError() {
return this.#error;
}
getValue() {
return this.#value;
}
getError() {
return this.#error;
}
}
function getName(name) {
if (!name) {
return Result.ofError(new Error("name is empty"));
}
return Result.ofValue(name);
}
function displayName() {
const result = getName("νκΈΈλ");
if (result.hasError()) {
console.error(result.getError());
} else {
console.log("my name is " + result.getValue());
}
}
displayName();
- μμμ»΄ λ°ν μ ν: Resultλ μμ μ λν μνκ²°κ³ΌλΌκ³ νλ€λ©΄, μμμ»΄μ μνμ λν μ±κ³΅μ¬λΆμ λνκ²μ μ리λκ²μ κ°κΉλ€
class Mission {
#missionId;
constructor(missionId) {
this.#missionId = missionId;
}
isOpen() {
if (this.#missionId === "FOOTBALL") return false;
else return true;
}
participate() {
// λ―Έμ
μνλ‘μ§..
}
}
/**
* @param {Mission} mission
* @returns {Boolean} // λΆλ¦°μ λ°λΌ μ±κ³΅μ¬λΆλ₯Ό 리ν΄νλ€
*/
function participateMission(mission) {
if (mission.isOpen()) {
mission.participate();
return true; // λ―Έμ
μ°Έμ¬κ°λ₯νλ©΄ True
}
return false; // λ―Έμ
μ°Έμ¬ λΆκ°λ₯νλ©΄ False
}
const result = participateMission(new Mission("BASEBALL"));
console.log(result);
μμμ λ°©λ²
π‘μ½λλ₯Ό νΈμΆνλ μͺ½μμ μ리μ§λ§, κ·Έ μ€λ₯λ₯Ό μ κ²½μ°μ§ μμλ λλ€
- λΉκ²μ¬ μμΈ: μ½λλ₯Ό νΈμΆνλ μͺ½μμ μΈμ§ λͺ»ν κ°λ₯μ±μ΄ λλ€
class NegativeNumberException extends Error { // λΉκ²μ¬ μμΈ μ νμμ λνλ΄λ ν΄λμ€
#erroneousNumber;
constructor(erroneousNumber) {
super(`μμλ νμ©λμ§ μμ΅λλ€: ${erroneousNumber}`);
this.name = "NegativeNumberException";
this.#erroneousNumber = erroneousNumber;
}
getErroneousNumber() {
return this.#erroneousNumber;
}
}
function getSquareRoot(value) {
if (value < 0) {
// μ€λ₯μ λν΄ λΉκ²μ¬ μμΈ λ°μ
throw new NegativeNumberException(value);
}
return Math.sqrt(value);
}
function displaySquareRoot() {
value = -1;
try {
getSquareRoot(value);
} catch (error) {
// λΉκ²μ¬ μμΈ μ²λ¦¬
console.error("[displaySquareRoot error] ", error);
}
}
displaySquareRoot();
- νλ‘λ―Έμ€ λλ Future : Promiseννλ‘ μ€λ₯λ₯Ό λ°νν μ μλ€. λ€λ§ νΈμΆνλμͺ½μμλ Promise μ€ν¨μ λν μΈλΆμ μΈ λ΄μ©μ μμμΌ νλ€
/**
*
* @param {String} message
* @returns {Promise<String>}
*/
function sendMessage(message) {
return new Promise((resolve, reject) => {
if (!message || !message.trim()) {
reject(new Error("μ λ¬ν λ©μΈμ§κ° μμ΅λλ€"));
}
resolve("λ©μΈμ§ μ μ‘μ μ±κ³΅νμμ΅λλ€");
});
}
sendMessage()
.then(() => {
console.log("success send message");
})
.catch((err) => {
console.error(`[sendMessage error] : ${err}`);
});
- λ§€μ§κ° λ°ν: κ°μ μλ―Έλ₯Ό μκΈ° μν΄μλ λ¬Έμλ₯Ό μ½μ΄μΌ νλ―λ‘, μμμ μ λ¬ κΈ°λ²μ΄λ€.
function getSquareRoot(value) {
if (value < 0) {
return -1; // λ§€μ§κ° λ°ν λ€λ§, μ΄λ€ μ€λ₯μΈμ§λ νμΈνκΈ° μ΄λ ΅λ€
}
return Math.sqrt(value);
}
λ°μν
'Javascript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| π μ’μμ½λ λμμ½λ 6μ₯ μμΈ‘ κ°λ₯ν μ½λλ₯Ό μμ±νλΌ (0) | 2026.08.31 |
|---|---|
| Promise.all / Promise.allSettled (0) | 2026.05.26 |
| Web-Client DIP(Dependency inversion principle) (0) | 2026.05.15 |
| π DIP (0) | 2026.05.11 |
| QueueμΌλ‘ μμ μ€ν (0) | 2025.05.19 |
λκΈ