
๐ฏ JavaScript์ ์ ๋ฐ์ ์ธ ๊ฐ๋ ์ ๋ํด ์์๋ด ๋๋ค.
Flow control์ ๋ช ๋ นํ ํ๋ก๊ทธ๋จ์ ๊ฐ๋ณ ๋ช ๋ น๋ฌธ, ๋ช ๋ น ๋๋ ํจ์ ํธ์ถ์ด ์คํ๋๊ฑฐ๋ ํ๊ฐ๋๋ ์์๋ฅผ ๋ํ๋ ๋๋ค.
์ผ๋ถ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋๋ ๊ฒฝ์ฐ์๋ง ์ผ๋ จ์ ๋ช ๋ น๋ฌธ์ ์คํํฉ๋๋ค.
๋ ผ๋ฆฌ์กฐ๊ฑด์ ์ฐธ/๊ฑฐ์ง์ ๋ฐ๋ผ ๋ช ๋ น๋ฌธ์ ์คํํด์ผ ํ ๊ฒฝ์ฐ ์ฌ์ฉํฉ๋๋ค.
if๋ฌธ์ ๋
ผ๋ฆฌ์กฐ๊ฑด์๋ true, false๋ก ํ๊ฐํ ์ ์๋ ํํ์์ ๋์
๊ฐ๋ฅํฉ๋๋ค.
๋
ผ๋ฆฌ์กฐ๊ฑด์ด ์ฐธ์ธ ๊ฒฝ์ฐ, if ๋ธ๋ญ๋ฌธ์ ์คํํ๊ณ , ๊ฑฐ์ง์ธ ๊ฒฝ์ฐ, else ๋ธ๋ญ๋ฌธ์ ์ฌ์ฉํฉ๋๋ค.
false ๋ก ํ๊ฐ๋๋ ๊ฐ : false, undefined, null, 0, NaN, ""
else if๋ฅผ ์ฌ์ฉํ์ฌ, ๋ค์์ ์กฐ๊ฑด์ ์์ฐจ์ ์ผ๋ก ๊ฒ์ฌํ ์ ์์ต๋๋ค.
๐ค ๋น ๋ฅด๊ฒ
if๋ฌธ์ ๋๋ผ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์๊น?
if...else๋ฌธ์ด ์๋ ์ค์ฒฉif๋ฌธ์ ์ฌ์ฉํ์ฌ nested contidional์ ์ง์ํ ์ ์์ต๋๋ค.
beforeconst ์กฐ๊ฑด๋ฌธ = (์ธ์) => ์ธ์ === 1; const ์กฐ๊ฑด๋ฌธ2 = (์ธ์) => ์ธ์ === 2; const ํจ์ = (์ธ์) => { if (์กฐ๊ฑด๋ฌธ(์ธ์)) { return 1; } else if (์กฐ๊ฑด๋ฌธ2(์ธ์)) { return 2; } else return 3; }; ํจ์(2);
afterconst ์กฐ๊ฑด๋ฌธ = (์ธ์) => ์ธ์ === 1; const ์กฐ๊ฑด๋ฌธ2 = (์ธ์) => ์ธ์ === 2; const ํจ์ = (์ธ์) => { if (์กฐ๊ฑด๋ฌธ(์ธ์)) return 1; if (์กฐ๊ฑด๋ฌธ2(์ธ์)) return 2; return 3; }; ํจ์(2);
switch์ ๋ช ์๋ ํํ์์ ํ๊ฐํ ํ, ํ๊ฐ๋ ๊ฐ๊ณผ case ๋ผ๋ฒจ ๊ฐ์ ๋น๊ตํ์ฌ ์ผ์นํ๋ case ๋ช ๋ น๋ฌธ์ ์คํํฉ๋๋ค.
ํ๊ฐ๋ ๊ฐ์ ํด๋นํ๋ case๋ฌด์ด ์์ ๊ฒฝ์ฐ default์ ๋ช ๋ น๋ฌธ์ด ์คํ๋๋๋ก default๋ฌธ์ ์์ฑํ ์ ์์ต๋๋ค.
const foo = (parameter) => {
switch (parameter) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
return 0;
}
};
foo(3); // 3
์ด๋ค ์กฐ๊ฑด์ด ์ถฉ์กฑ๋ ๋๊น์ง ์ผ๋ จ์ ๋ช ๋ น๋ฌธ์ 0ํ ์ด์ ์คํํฉ๋๋ค.
์กฐ๊ฑด๋ถ loop๋ ์ข ๋ฃ๋ ์ ์๋ ์กฐ๊ฑด์ด ์๋ ๊ฒฝ์ฐ์๋ ๋ฌดํ ๋ฃจํ์ ๋น ์ง ์ ์์ผ๋ฏ๋ก ์ฃผ์ํด์ผํฉ๋๋ค.
let count = 0;
while (count > 3) {
count++;
console.log(count);
}
console.log('end');
// 1
// 2
// 3
// 'end'
let count = 0;
do {
count++;
console.log(count);
} while (count < 3);
console.log('end');
ํน์ ๋ถ๋ถ ์ฝ๋๊ฐ ๋ฐ๋ณต์ ์ผ๋ก ์ํ๋ ์ ์๋๋ก ํฉ๋๋ค.
for
์ด๊ธฐ๋ฌธ : loop ๋ด์ ์ฌ์ฉํ loop๋ณ์๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
์กฐ๊ฑด๋ฌธ : loop ๋ด ์ฝ๋๋ฅผ ์คํํ๊ธฐ ์ ์ ์กฐ๊ฑด์ ํ๊ฐํ์ฌ loop๋ฅผ ์ง์ํ ์ง ํ๋จํฉ๋๋ค.
์ฆ๊ฐ๋ฌธ : loop ๋ด ์ฝ๋๋ฅผ ์คํํ ํ, ์คํ๋๋ ๋ฌธ์ฅ์ ๋๋ค.
for (let ๋ฃจํ๋ณ์ = 0; ๋ฃจํ๋ณ์ < 3; ๋ฃจํ๋ณ์++) {
console.log(๋ฃจํ๋ณ์);
}
์ปฌ๋ ์
์์ ํญ๋ชฉ๋ค์ ํก๋จํ๋ ์ ์ดํ๋ฆ๋ฌธ์
๋๋ค. for๋ฌธ๊ณผ ๋ค๋ฅด๊ฒ ๋ช
์์ ์ผ๋ก ๋ฃจํ๋ณ์๋ฅผ ๊ด๋ฆฌํ์ง ์์ต๋๋ค.
const foo = {
apple: '๐',
banana: '๐'
};
for (const key in foo) {
console.log(key);
}
// 'apple'
// 'banana'
const foo = 'apple';
for (const i of foo) {
console.log(i);
}
// 'a'
// 'p'
// 'p'
// 'l'
// 'e'
while, do...while, for๋ฌธ์ ๋ค์ ์์ํ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค.while, do...while, for๋ฌธ์ ๋๋ฌ์ผ ๋ฐ๋ณต์ ์ข
๋ฃํ๊ณ , ๋ค์ loop๋ฅผ ์คํํฉ๋๋ค.const foo = () => {
let count = 0;
while (count < 3) {
count++;
console.log(count);
continue;
console.log('์ถ๋ ฅ๋์ง ์๋ ๋ฌธ์์ด');
}
};
foo();
// 1
// 2
// 3
switch๋ฌธ์ ์ข
๋ฃ์ํฌ ๋ ์ฌ์ฉ๋ฉ๋๋ค.while, do-while, for, switch๋ฌธ์ ์ข
๋ฃํ๊ณ , ๋ค์ ๋ช
๋ น์ด๋ก ๋์ด๊ฐ๊ฒ ํฉ๋๋ค.const foo = (type) => {
switch (type) {
case 'apple':
console.log('๐');
case 'banana':
console.log('๐');
break;
default:
console.log('๐ค');
}
console.log('last');
};
foo('apple');
// '๐' ๊ฐ ์ถ๋ ฅ๋๊ณ , break๊ฐ ์๊ธฐ ๋๋ฌธ์ 'banana'๋ก ๋์ด๊ฐ๋๋ค.
// '๐' ๊ฐ ์ถ๋ ฅ๋๊ณ , break๊ฐ ์๊ธฐ ๋๋ฌธ์ default๋ ์คํ๋์ง ์์ต๋๋ค.
// ๋ฐ๋ณต๋ฌธ์ ๋์จ ํ, 'last'๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
return์ ๋ง๋๋ฉด ํจ์๋ ์ฆ์ ์ข
๋ฃ๋๋ฉฐ, ์ดํ์ ์ฝ๋๋ ์คํ๋์ง ์์ต๋๋ค.const findEvenNumber = (arr) => {
for (let num of arr) {
if (num % 2 === 0) {
return num;
}
}
return '์ง์๊ฐ ์์';
};
console.log(findEvenNumber([1, 3, 5, 6, 7])); // 6
์์ธ๋ฅผ ๋ฐ์์ํฌ ๋ ์ฌ์ฉ๋ฉ๋๋ค. catch ๋ธ๋ญ์์ ์๋ฌ ๊ฐ์ฒด๋ฅผ ํธ๋ค๋งํ๊ฒ ๋ฉ๋๋ค.
์์ธ๊ฐ ๋ฐ์ ์ ๊ณผ์
ํ์ฌ ํจ์์ ์คํ์ด ์ค์ง๋ฉ๋๋ค.
์๋ฌ ๊ฐ์ฒด์ ํจ๊ป ์๋ฌ๊ฐ throw ๋ฉ๋๋ค.
์ ์ด ํ๋ฆ์ catch ๋ธ๋ก์ด ์์ผ๋ฉด ์ ๋ฌ๋๊ณ , ์์ผ๋ฉด ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋ฉ๋๋ค.
const foo = () => {
console.log(1);
throw '์ฌ๊ธฐ';
console.log(2); // throw๋ฌธ ์ดํ์ ๋ช
๋ น๋ฌธ์ ์คํ๋์ง ์์ต๋๋ค.
};
foo();
// 1
// Error: ์ฌ๊ธฐ
์ฌ์ฉ์๊ฐ ์ง์ Error ๊ฐ์ฒด๋ฅผ ์ ์ํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค.
const foo = () => {
console.log(1);
const customError = new Error();
customError.name = 'ErrorName';
customError.message = '์๋ฌ ๋ฐ์';
throw customError;
console.log(2);
};
foo();
// 1
// ErrorName : ์๋ฌ ๋ฐ์
์ฝ๋ ์คํ ์ค ๋ฐ์ํ๋ ์ค๋ฅ๋ฅผ ์ก์๋ด์ด ํ๋ก๊ทธ๋จ์ด ๊ฐ์ ์ข ๋ฃ๋์ง ์๋๋ก ํ๋ ์์ธ ์ฒ๋ฆฌ ๊ตฌ๋ฌธ์ ๋๋ค.
const foo = (value) => {
if (value < 3) throw value;
else console.log(value);
};
const bar = (value) => {
try{
foo(value);
} catch (catchID) {
console.log('catch', catchID);
} finally {
console.log('finally'); // ์์ธ ์ํฉ์ด ๋ฐ์ํ์ง ์๋๋ผ๋ ์ถ๋ ฅ๋ฉ๋๋ค.
}
};
bar(2);
// 'catch' 2
// 'finally'
๊ฐ์ฒด๋ ์์ฑ์ ๊ฐ์ง ๋ ๋ฆฝ์ ์ธ ๊ฐ์ฒด์ ๋๋ค.
const foo = { // ๋ฆฌํฐ๋ด ํ๊ธฐ
hello : 'JavaScript',
happy : 'to meet you',
};
function Person (name, age) { // ์์ฑ์ ํจ์
this.name = name;
this.age = age;
}
new Person('Jason', 30);
const Person2 = { // Object.create
name: 'Mickey',
age: 25,
getName: function () {
console.log(this.name);
},
};
const Joy = Object.create(Person2);
Joy.name = 'Joy';
Joy.getName(); // 'Joy'
์์ฑ
property: ํค์ ๊ฐ ์ฌ์ด์ ์ฐ๊ฒฐ๊ด๊ณ์
๋๋ค.
method: ๊ฐ์ฒด์ ์ํด์๋ ํจ์๋ฅผ ์ผ์ปซ๋ ๋ง์
๋๋ค.
์์ฑ ๋์ด ๋ฐฉ๋ฒ
for...in
Object.keys(๊ฐ์ฒด)
Object.getOwnPropertyNames(๊ฐ์ฒด)
const foo = {
name: 'apple'
};
const bar = {
name: 'apple'
};
console.log(foo === bar); // false
const person = {
name : 'Amy',
age : 30,
};
// ์์ ๋ณต์ฌ ๋ฐฉ๋ฒ 2๊ฐ์ง
const joy = Object.assign({}, person);
const kei = { ...person };
joy.name = 'Joy';
kei.name = 'Kei';
console.log(joy === kei); // false
console.log(joy.age === kei.age); // true;
๊น์ ๋ณต์ฌ
๋ณต์ฌ๋ ๊ฐ์ฒด์ ์์ฑ ์ค ํ๋๋ผ๋ ๊ฐ์ ์ฐธ์กฐ๋ฅผ ํ๊ณ ์๋ ์์ฑ์ด ์๋ค๋ฉด ๊น์ ๋ณต์ฌ๋ผ๊ณ ํฉ๋๋ค.
์ฌ๊ทํจ์๋ฅผ ์ด์ฉํ ๋ณต์ฌ
JSON.stringify
์ซ์๋ฅผ ํํํ๊ณ ๋ค๋ฃฐ ๋ ์ฌ์ฉํ๋ ์์ ๋ํผ Bulit-in ๊ฐ์ฒด์ ๋๋ค.
์์ฑ์ ํจ์
Number.prototype
์ธ์คํด์ค ๋ฉ์๋: toFixed, toLocalString, ...
์ํ์ ์ธ ์์์ ํจ์๋ฅผ ์ํ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ๊ฐ์ง Built-in ๊ฐ์ฒด์ ๋๋ค.
์์ฑ : PI, E, ...
๋ฉ์๋ : abs(), pow(), ceil(), ...
1970๋ 1์ 1์ผ UTC ์์ ๊ณผ์ ์๊ฐ ์ฐจ์ด๋ฅผ ๋ฐ๋ฆฌ์ด ๋จ์๋ก ๋ํ๋ธ ๊ฒ์ ๋๋ค. ํ๊ตญ์ UTC ๊ธฐ์ค์ผ๋ก 9์๊ฐ์ด ๋ํด์ง๋๋ค.
๐ค UTC๋ ๋ฌด์์ผ๊น?
UTC๋ ๊ตญ์ ํ์ค ์๊ฐ์ ํ์ ์ธ๊ณ์๋ก ๊ตญ์ ์ ์ธ ํ์ค ์๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ฐ์ด๋ ์๊ฐ์ ๋๋ค. 1970๋ ๋์ UTC ์ฑํ์ด ๋๊ณ , ๊ทธ๋ฆฌ๋์น ํ๊ท ์(GMT)์ ๊ธฐ๋ฐํฉ๋๋ค.
์์ฑ: x
๋ฉ์๋: now(), parse(), UTC()
const example = [
Date.now(),
Date.parse('March 27, 2025'),
Date.UTC(2025, 03, 27),
];
example.forEach((item) => console.log(item));
// 1743087736192
// 1743087600000
// 1745798400000
const year = new Date().getFullYear() // 2025
const month = new Date().getMonth()+1; // 3, 1์์ด 0๋ถํฐ ์์ํ๊ธฐ ๋๋ฌธ์ 1์ ๋ํด ์ค
const day = new Date().getDate(); // 27
new Date().toLocaleString(); // '2025. 3. 27. ์คํ 06:26:14'
new Date().toLocaleDateString(); // '2025. 3. 27.'
๋ฌธ์์ด ๋ฆฌํฐ๋ด๋ก ํํ๋๋ฉฐ, ์๋ฐ์ดํ("), ๋ฐ์ดํ('), ๋ฐฑํฑ(`)์ ์ฌ์ฉํ์ฌ ํํํฉ๋๋ค.
์ธ์คํด์ค ์์ฑ : length
์ธ์คํด์ค ๋ฉ์๋
์ ๊ทผ : at(), charAt(), charCodeAt(), codePointAt()
์ฐพ๊ธฐ : indexOf(), lastIndexOf(), search()
ํฌํจ ์ฌ๋ถ: includes(), startWith(), endsWith(), match(์ ๊ทํํ์), matchAll(์ ๊ทํํ์)
๋น๊ต: localeCompare()
๋ณ๊ฒฝํ๊ธฐ: toLocaleLowerCase(), toLocaleUpperCase(), toString(), replace(), replaceAll()
์ถ๊ฐํ๊ธฐ: padEnd(), padStart(), repeat()
ํฉ์น๊ธฐ: concat(), +
๋ถ๋ฆฌํ๊ธฐ: slice, substring(), split(), trim(), trimStart(), trimEnd()
const str = "Hello, ๐!";
console.log(str.at(1)); // "e"
console.log(str.charAt(1)); // "e"
console.log(str.charCodeAt(1)); // 101 (Unicode for 'e')
console.log(str.codePointAt(7)); // 127757 (Unicode for '๐')
// ------------------------------------
const text = "Hello world, welcome to the universe.";
console.log(text.indexOf("world")); // 6
console.log(text.lastIndexOf("o")); // 25
console.log(text.search(/welcome/i)); // 13 (๋์๋ฌธ์ ๋ฌด์ ์ ๊ทํํ์)
// ------------------------------------
const sentence = "JavaScript is awesome!";
console.log(sentence.includes("awesome")); // true
console.log(sentence.startsWith("Java")); // true
console.log(sentence.endsWith("!")); // true
console.log(sentence.match(/\w+/g)); // ["JavaScript", "is", "awesome"]
console.log([...sentence.matchAll(/\w+/g)]);
// [ ["JavaScript"], ["is"], ["awesome"] ]
// ------------------------------------
console.log("apple".localeCompare("banana")); // -1 (์ ๋ฌธ์์ด์ด ์ฌ์ ์์ผ๋ก ๋ ์)
console.log("banana".localeCompare("apple")); // 1 (๋ ๋ค)
console.log("apple".localeCompare("apple")); // 0 (๊ฐ์)
// ------------------------------------
const phrase = "Hello, How Are You?";
console.log(phrase.toLocaleLowerCase()); // "hello, how are you?"
console.log(phrase.toLocaleUpperCase()); // "HELLO, HOW ARE YOU?"
const number = 2025;
console.log(number.toString()); // "2025"
const message = "The sun is bright. The sun gives us light.";
console.log(message.replace("sun", "moon"));
// "The moon is bright. The sun gives us light."
console.log(message.replaceAll("sun", "moon"));
// "The moon is bright. The moon gives us light."
// ------------------------------------
const num = "5";
console.log(num.padStart(3, "0")); // "005"
console.log(num.padEnd(3, "0")); // "500"
console.log("Hi! ".repeat(3)); // "Hi! Hi! Hi! "
// ------------------------------------
const text = " Hello, JavaScript! ";
console.log(text.slice(2, 8)); // "Hello,"
console.log(text.slice(-10, -1)); // "JavaScript"
console.log(text.substring(2, 8)); // "Hello,"
console.log(text.substring(8, 2)); // "Hello," (start > end๋ฉด ์๋์ผ๋ก ๊ต์ฒด)
console.log(text.split(" ")); // ["", "", "Hello,", "JavaScript!", "", ""]
console.log(text.split(",", 2)); // [" Hello", " JavaScript! "]
console.log(text.trim()); // "Hello, JavaScript!"
console.log(text.trimStart()); // "Hello, JavaScript! "
console.log(text.trimEnd()); // " Hello, JavaScript!"
๋ฌธ์์ด์์ ํน์ ๋ฌธ์์ ์กฐํฉ ํจํด์ ์ฐพ๋ ๋๊ตฌ์ ๋๋ค. ์ฌ๋์(/)๋ก ๊ฐ์ผ ํํ์ด๋ฉฐ, ํ๋๊ทธ(flags)๋ ์ ํ ์ฌํญ์ ๋๋ค.
/ํจํด(pattern)/ํ๋๊ทธ(flags)
๐ ์ ๊ทํํ์์ ๋ฏธ๋ฆฌ ์์ฑํ ์ ๋ธ๋ก๊ทธ๋ฅผ ํตํด ๊ฐ๋
์ ํ์ธํด์ฃผ์ธ์!
Collection์ ๋ฐ์ดํฐ ํญ๋ชฉ๋ค์ ๊ทธ๋ฃน์ ์๋ฏธํฉ๋๋ค.
๋ฐฐ์ด์ ์ฌ๋ฌ ๊ฐ์ ์์๋๋ก ์ ์ฅํ ์ ์๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ์
๋๋ค. ๋ฐฐ์ด ๋ฆฌํฐ๋ด๋ก ํํ๋๋ฉฐ, ๋๊ดํธ([])๋ก ๊ฐ์ธ์ ์ ์ํฉ๋๋ค.
์ธ์คํด์ค ์์ฑ: length
์ธ์คํด์ค ๋ฉ์๋
์์ ์ ๊ทผ: at()
์์ ์ฐพ๊ธฐ : indexOf(), lastIndexOf()
์์ ํฌํจ ์ฌ๋ถ : includes
์์ ํ์ธํ๊ธฐ: forEach()
์์ ํ๋ณํ๊ธฐ : every(), some()
์์ ์ฐพ๊ธฐ : find(), findIndex()
์์ ์ถ๊ฐํ๊ธฐ : push(), unshift()
์์ ์ ๊ฑฐํ๊ธฐ : pop(), shift()
์์ ๋ณ๊ฒฝํ๊ธฐ : flatMap(), map()
์์ ์ ๊ฑฐํ๊ธฐ : filter()
๋ฐฐ์ด ํฉ์น๊ธฐ : concat(), fill()
๋ฐฐ์ด ๋ถ๋ฆฌํ๊ธฐ : slice(), splice()
๋ฐฐ์ด ์ฌ๋ฐฐ์นํ๊ธฐ : reverse(), sort(),
ํค์ ๊ฐ์ ์๋ก ๋งคํ์์ผ ์ ์ฅํ๊ณ ์ ์ฅ๋ ์์๋๋ก ์์๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์ ๊ทผํ ์ ์๋ ๊ฐ์ฒด์ ๋๋ค.
๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ํค๋ก ํ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ๊ณ ์๋ ์์ฑ์ด ์์ด ๊ธฐ๋ณธ ์์ฑ๋ค๊ณผ์ ์ถฉ๋์ด ์์ต๋๋ค.
์ถ๊ฐ๋ ์์๋๋ก ๋ฐ๋ณต๋ฉ๋๋ค.
ํค, ๊ฐ์์ ๋น๋ฒํ ์ถ๊ฐ์ ๊ฑฐ์์๋ Object๋ณด๋ค Map์ด ํผํฌ๋จผ์ค๊ฐ ์ข์ต๋๋ค.
์ค๋ณต๋ ๊ฐ์ ํ์ฉํ์ง ์๋ ๊ฐ์ฒด์ ๋๋ค.
JavaScript Object Notation์ผ๋ก ์ธํฐ๋ท์์ ์๋ฃ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ๋ ์๋ฃ๋ฅผ ํํํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
์ ์ ๋ฉ์๋
JSON.stringify: ๋ฌธ์์ด๋ก ๋ณํํฉ๋๋ค.
JSON.parse: ๋ฌธ์์ด์์ JSON์ผ๋ก ๋ณํํฉ๋๋ค.
try...catch๋ก ์๋ฌ ํธ๋ค๋ง์ด ๊ถ์ฅ๋ฉ๋๋ค.๊ฐ์ฒด๊ฐ ๋ค๋ฅธ ๊ฐ์ฒด์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์์ํ ์ ์๊ฒ ํ๋ ๋ฉ์ปค๋์ฆ์ ๋๋ค.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log(`Hello, my name is ${this.name}`);
};
const person1 = new Person('John');
person1.greet(); // "Hello, my name is John"
console.log(person1.constructor); // Person
ํจ์ ํ์
์๋ง prototype๊ฐ ์กด์ฌํฉ๋๋ค.
ํจ์์ ๋ถ๋ชจ๊ฐ์ฒด๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ๋ถ๋ชจ ๊ฐ์ฒด์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
๊ฐ์ฒด๋ [[Prototype]]์ด๋ผ๋ ๋ด๋ถ ํ๋กํผํฐ๋ฅผ ํตํด ํ๋กํ ํ์
์ ๊ฐ๋ฆฌํด. ์ด๋ฅผ __proto__๋ก ์ ๊ทผํ ์ ์์ต๋๋ค. (๊ถ์ฅ X)
constructor : ๊ฐ์ฒด๋ฅผ ์์ฑํ ์์ฑ์ ํจ์๋ฅผ ๊ฐ๋ฆฌํค๋ ํน์ํ ์์ฑ์
๋๋ค.
ํ๋กํ ํ์ ๊ธฐ๋ฐ ์์์ ์ฌ์ฉํ๋ ๋ฐฉ์์ ๋๋ค. ํ๋กํ ํ์ ์ ๋ฌธ๋ฒ์ ์คํ(syntactic sugar)์ด๋ผ๊ณ ํ ์ ์์ต๋๋ค.
์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ํด๋์ค ํ๋๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํด ์ฝ์๋ ํน์ ๋ฉ์๋์ ๋๋ค. ์๋ต์ด ๊ฐ๋ฅํฉ๋๋ค.
ํด๋์ค ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์์๋ ํธ์ถํ ์ ์๋ ๋ฉ์๋๋ฅผ ์ ์ํ ๋ ์ฌ์ฉํฉ๋๋ค. static ํค์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
ํด๋์ค ๋ด๋ถ์ ์บก์ํ๋ ๋ณ์ = ๋ฉค๋ฒ ๋ณ์๋ฅผ ๋งํฉ๋๋ค. ์ธ์คํด์ค ์์ฑ์ this์ ๋ฐ์ธ๋ฉ์ด ํ์ํฉ๋๋ค.
ํน์ ์ธ์คํด์ค ์์ฑ์ ์กฐํํ๋ฉฐ ์กฐ์ํ๋ ๋ฉ์๋์
๋๋ค. get ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉฐ ๋ฌด์กฐ๊ฑด ๊ฐ์ ๋ฐํํฉ๋๋ค.
ํน์ ์ธ์คํด์ค ์์ฑ์ ํ ๋นํ๋ฉฐ ์กฐ์ํ๋ ๋ฉ์๋์
๋๋ค. set ํค์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
extends ์ super ํค์๋๋ฅผ ํตํด class์์ ์์์ ๊ตฌํํ ์ ์์ต๋๋ค.
class Person {
constructor(name, age) {
this.name = name; // ์ธ์คํด์ค ์์ฑ
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
<}
get fullName() {
return `${this.name} Smith`;
}
set fullName(value) {
const [firstName] = value.split(' ');
this.name = firstName;
}
static describe() {
console.log("This is a Person class.");
}
}
class Student extends Person {
constructor(name, age, major) {
super(name, age); // ๋ถ๋ชจ ํด๋์ค์ constructor ํธ์ถ
this.major = major;
}
greet() {
super.greet(); // ๋ถ๋ชจ ํด๋์ค์ greet ํธ์ถ
console.log(`I major in ${this.major}.`);
}
}
const student1 = new Student('Alice', 22, 'Computer Science');
student1.greet(); // "Hello, my name is Alice and I am 22 years old. I major in Computer Science."
Person.describe(); // "This is a Person class."
student1.fullName = "Bob";
console.log(student1.fullName); // "Bob Smith"
ํ์ฌ ์ด๋์ ์ฝ๋๋ฅผ ์คํํ๊ณ ์๋์ง(์คํ ์ปจํ
์คํธ)๋ฅผ ๊ฐ๋ฆฌํต๋๋ค. ํจ์๋ฅผ ์ด๋์์ ํธ์ถ ํ๋์ง์ ๋ฐ๋ผ this์ ๊ฐ์ด ๋ฌ๋ผ์ง๋๋ค.
๐ this์ ๋ฏธ๋ฆฌ ์์ฑํ ์ ๋ธ๋ก๊ทธ๋ฅผ ํตํด ๊ฐ๋ ์ ํ์ธํด์ฃผ์ธ์!
๋ณ์์ ํ๋ ๋ฒ์์ ๋๋ค. ์ฃผ๋ก ์ธ๋ถ์์ ๋ด๋ถ์ ์ ๊ทผํ ์ ์๋๋ก ๋ณดํธํ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค.
๐ ์ค์ฝํ์ ๋ฏธ๋ฆฌ ์์ฑํ ์ ๋ธ๋ก๊ทธ๋ฅผ ํตํด ๊ฐ๋ ์ ํ์ธํด์ฃผ์ธ์!
์คํ ์ค์ธ ์ฝ๋์ ์คํ ํ๊ฒฝ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด์ ๋๋ค. ์ฝ๋๋ฅผ ์คํํ ๋, ๊ฐ ์ฝ๋ ๋ธ๋ก์ด ์ด๋ค ํ๊ฒฝ์์ ์คํ๋๊ณ ์๋์ง ๊ด๋ฆฌํ๋ ์ค์ํ ์ญํ ์ ํฉ๋๋ค.
๐ ์คํ ์ปจํ ์คํธ๋ ๋ฏธ๋ฆฌ ์์ฑํ ์ ๋ธ๋ก๊ทธ๋ฅผ ํตํด ๊ฐ๋ ์ ํ์ธํด์ฃผ์ธ์!
ํจ์ ์์ ํจ์์์ ์๊ธฐ๋ ๊ด๊ณ์ ๋๋ค. ์ฃผ๋ก ์ธ๋ถ์์ ๋ด๋ถ์ ์ ๊ทผํ ์ ์๋๋ก ๋ณดํธํ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค.
๐ ํด๋ก์ ๋ ๋ฏธ๋ฆฌ ์์ฑํ ์ ๋ธ๋ก๊ทธ๋ฅผ ํตํด ๊ฐ๋ ์ ํ์ธํด์ฃผ์ธ์!
ํ๋ฃจ์ ๋ง์ ๊ฐ๋
๋ค์ ์ ๋ฆฌํ๋๋ผ ํ๋ค์๋ค. ๊ทธ๋ฆฌ๊ณ ์์ธ ์ํฉ์์๋ ํ๋ก๊ทธ๋จ์ด ๋ฉ์ถ์ง ์๋๋ก try...catch๋ก ํ๋ฆ์ ์ ์ดํ๋ ๋ฐ ์ต์ํด์ ธ์ผ๊ฒ ๋ค๊ณ ๋๊ผ๋ค.
