1. ํด๋์ค(class)
์์ฑ์ ํจ์๋ โ๋ถ์ด๋นต ๊ธฐ๊ณโ์ฒ๋ผ ๊ฐ์ฒด๋ฅผ ์ฐ์ด๋ผ ์ ์๋ โํ ํ๋ฆฟโ ์์ ๊ฐ์ ๊ฐ๋ ์ด๊ณ ,๊ฐ์ฒด๋ ํ ํ๋ฆฟ์ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ฃผ์ ํด์ ๋ง๋ โ๋ถ์ด๋นตโ์ ๋น์ ํ ์ ์์ต๋๋ค.
์ด๊ฒ์ด ๊ฐ๋ฅํ ์ด์ ๋ JavaScript์์๋ ํ๋กํ ํ์
(Prototype)์ ํ ๋๋ก ํด์ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ์ง์ํด์ฃผ๊ธฐ ๋๋ฌธ์
๋๋ค.
๋๋ค์ ๊ฐ์ฒด ์งํฅ ์ธ์ด๋ค์ ํ๋กํ ํ์
๋ณด๋ค ํด๋์ค(Class)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋์ํฉ๋๋ค.
JavaScript์ ๋ด๋ถ์์๋ ํ๋กํ ํ์
์ ์ฐ๊ณ , ๊ฐ๋ฐ์๋ค์ ํด๋์ค๋ฅผ ์ด์ฉํด๋ ๋ด๋ถ์์๋ ํ๋กํ ํ์
์ ์ด์ฉํ๊ธฐ ๋๋ฌธ์
๋๋ค.
์ฐธ๊ณ ๋ก ES6+ ์ด์์ JavaScript์์ Class ๋ฌธ๋ฒ์ ์ง์ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํ์ ์ ๋๋ถ๋ถ ์ธ์ด๋ ํด๋์ค๋ฅผ ์ฌ์ฉํ๊ณ , ์์ฑ์ ํจ์๋ฅผ ๋ ์ด์ ์ฌ์ฉํ๊ณ ์์ง ์์ต๋๋ค.
๐ก ์ฉ์ด ์ ๋ฆฌ
ํด๋์ค(Class): ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ ํ ํ๋ฆฟ (e.g. ์ฒญ์ฌ์ง, ํ, ์์, ์ค๊ณ๋)
- ํน์ ๊ฐ์ฒด(์ธ์คํด์ค)๋ฅผ ์์ฑํ๊ธฐ ์ํ ๋ณ์์ ๋ฉ์๋(ํจ์)๋ฅผ ์ ์ํ๋ ํ
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ(Object-Oriented Programming): ๊ฐ์ฒด๋ผ๋ฆฌ ์๋ก ํธํ๋๋๋ก ํ๋ก๊ทธ๋๋ฐํ๋ ๊ฒ์ธ์คํด์ค(Instance): ํด๋์ค๋ฅผ ํตํด ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์๋๋ฐ, ์ด๋ ํด๋์ค๋ฅผ ์ด์ฉํด ๋ง๋ค์ด์ง ๊ฐ์ฒด๋ค
1// ๊ฐ์ฒด๋ฅผ ์์ฝ๊ฒ ๋ง๋ค์ ์๋ ํ ํ๋ฆฟ2// 1. ์์ฑ์ ํจ์ (์ค๋๋ ๊ณ ์ ์ ์ธ ๋ฐฉ๋ฒ)3// 2. ํด๋์ค โจ4class Fruit {5// ๋ชจ๋ ํด๋์ค๋ ๊ธฐ๋ณธ ์์ฑ์(constructor)๊ฐ ์กด์ฌํด์ผ ํจ6// ์์ฑ์: new ํค์๋๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํธ์ถ๋๋ ํจ์7constructor(name, emoji) {8this.name = name9this.emoji = emoji10}1112display = () => {13console.log(`${this.name}: ${this.emoji}`)14}15}1617const apple = new Fruit('apple', '๐') // apple์ Fruit ํด๋์ค์ ์ธ์คํด์ค์ด๋ค.18const orange = new Fruit('orange', '๐') // orange์ Fruit ํด๋์ค์ ์ธ์คํด์ค์ด๋ค.1920console.log(apple) // Fruit { display: [Function: display], name: 'apple', emoji: '๐' }21console.log(orange) // Fruit { display: [Function: display], name: 'orange', emoji: '๐' }22console.log(apple.name)23console.log(apple.emoji)24apple.display()2526// obj๋ ๊ฐ์ฒด์ด๊ณ , ๊ทธ ์ด๋ค ํด๋์ค์ ์ธ์คํด์ค๋ ์๋๋ค.27const obj = { name: 'ellie' }
cf. MDN Class
2. ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๋ ๋ฐฉ๋ฒ(static)
ํด๋์ค์ ์ ์๋ ํ๋กํผํฐ์ ๋ฉ์๋๋ ์ธ์คํด์ค ๋ ๋ฒจ์ ํ๋กํผํฐ์ ๋ฉ์๋๋ผ๊ณ ํฉ๋๋ค.
์๋ํ๋ฉด ํด๋์ค์์ ์ ์ํ ํ๋กํผํฐ์ ํจ์๋ค์ด ์ธ์คํด์ค์ ์ค๋ณต์ ์ผ๋ก ๋ง๋ค์ด์ง๊ธฐ ๋๋ฌธ์
๋๋ค.
๊ฐ๊ฐ์ ์ธ์คํด์ค๋ค์ ํตํด ๊ฐ์ฒด.์์ฑ ๋๋ ๊ฐ์ฒด.ํจ์๋ก ํธ์ถํ ์ ์์ต๋๋ค.
๋ชจ๋ ๊ฐ์ฒด๋ง๋ค ๋์ผํ๊ฒ ์ฐธ์กฐํด์ผ ํ๋ ์์ฑ์ด๋ ํจ์๊ฐ ์๋ค๋ฉด, ํด๋์ค ๋ ๋ฒจ์ ํ๋กํผํฐ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
ํด๋์ค ์์ static ํค์๋๋ฅผ ํ๋กํผํฐ๋ ๋ฉ์๋ ์์ ์์ฑํ๋ฉด, ๋ง๋ค์ด์ง ์ธ์คํด์ค์ ํฌํจ๋์ง ์๊ณ ํด๋์ค์ ๊ทธ๋๋ก ๋จ์์๊ฒ ๋ฉ๋๋ค. ์ฆ, ํด๋์ค์ 1๋ฒ๋ง ์ ์ํ๊ณ ์ธ์คํด์ค์์ ์ฌ์ฌ์ฉ์ ํ๋ ๊ฒ์
๋๋ค.
๊ทธ๋์ ํธ์ถํ ๋๋๋ง๋ค์ด์ง ์ธ์คํด์ค.์ด ์๋๋ผ ํด๋์ค๋ช
.์ผ๋ก ํด๋์ค ๋ ๋ฒจ์ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค.
1// static ์ ์ ํ๋กํผํฐ, ๋ฉ์๋2class Fruit {3static MAX_FRUITS = 44// ์์ฑ์: new ํค์๋๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํธ์ถ๋๋ ํจ์5constructor(name, emoji) {6this.name = name7this.emoji = emoji8}910// ํด๋์ค ๋ ๋ฒจ์ ๋ฉ์๋11static makeRandomFruit() {12// ํด๋์ค ๋ ๋ฒจ์ ๋ฉ์๋์์๋ this๋ฅผ ์ฐธ์กฐํ ์ ์์13return new Fruit('banana', '๐')14}1516// ์ธ์คํด์ค ๋ ๋ฒจ์ ๋ฉ์๋17display = () => {18console.log(`${this.name}: ${this.emoji}`)19}20}2122const banana = Fruit.makeRandomFruit()23console.log(banana) // Fruit { display: [Function: display], name: 'banana', emoji: '๐' }24console.log(Fruit.MAX_FRUITS) // 42526// apple์ Fruit ํด๋์ค์ ์ธ์คํด์ค์ด๋ค.27const apple = new Fruit('apple', '๐')28// orange์ Fruit ํด๋์ค์ ์ธ์คํด์ค์ด๋ค.29const orange = new Fruit('orange', '๐')3031console.log(apple) // Fruit { display: [Function: display], name: 'apple', emoji: '๐' }32console.log(orange) // Fruit { display: [Function: display], name: 'orange', emoji: '๐' }33console.log(apple.name)34console.log(apple.emoji)35apple.display()3637Math.pow() // math ํด๋์ค API38Number.isFinite(1) // number ํด๋์ค API
3. ํ๋(field)
1// ์ ๊ทผ์ ์ด์ - ์บก์ํ2// private(#), public(๊ธฐ๋ณธ), protected3class Fruit {4// # : ๋ด๋ถ์์๋ ์ฌ์ฉ ๊ฐ๋ฅ, ์ธ๋ถ์์๋ ์ฌ์ฉ ๋ถ๊ฐ5#name6#emoji7#type = '๊ณผ์ผ'8constructor(name, emoji) {9this.#name = name10this.#emoji = emoji11}12#display = () => {13console.log(`${this.#name}: ${this.#emoji}`)14}15}1617const apple = new Fruit('apple', '๐')18// apple.#name = '์ค๋ ์ง'; // #field๋ ์ธ๋ถ์์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํจ19console.log(apple) // Fruit {}
4. getter์ setter
1// ์ ๊ทผ์ ํ๋กํผํฐ (Accessor Property)2class Student {3constructor(firstName, lastName) {4this.firstName = firstName5this.lastName = lastName6}78/*** Getter์ ์ญํ9* 1) ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํด์ ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ ๋10* 2) privateํ ๊ฐ์ ๋ฐํํ ๋11*/12// fullName์ ์ ๊ทผ(ํธ์ถ)ํ ๋13get fullName() {14return `${this.lastName} ${this.firstName}`15}1617/*** Setter์ ์ญํ18* ํ๋กํผํฐ๋ฅผ priavte๋ก ๋ง๋ค๋ฉด ์ง์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค.19* ์ด๋ด ๋, Setter๋ก ๊ฐ์ ๋ณ๊ฒฝํ๋ค.20* Setter๋ ๋ง์ด ์ฌ์ฉ๋์ง ์๋๋ค.21*/22// fullName์ ํ ๋นํ ๋23set fullName(value) {24console.log('set', value)25}26}2728const student = new Student('์์ง', '๊น')29student.firstName = '์๋'30console.log(student.firstName) // ์๋31console.log(student.fullName) // ๊น ์๋32student.fullName = '๊น์ฒ ์' // set ๊น์ฒ ์
5. ์์(inheritance)-super
superํค์๋ : ๋ถ๋ชจ์ ๊ธฐ๋ฅ์ ๊ทธ๋๋ก ์ ์งํ๋ฉด์ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ํ๊ณ ์ถ์ ๋
1class Animal {2constructor(color) {3this.color = color4}5eat() {6console.log('๋จน์!')7}8sleep() {9console.log('์๋ค')10}11}1213class Tiger extends Animal {}14const tiger = new Tiger('๋ ธ๋์ด')15console.log(tiger) // Tiger { color: '๋ ธ๋์ด' }16tiger.sleep() // ์๋ค17tiger.eat() // ๋จน์!1819class Dog extends Animal {20constructor(color, ownerName) {21super(color)22this.ownerName = ownerName23}24play() {25console.log('๋์์~!')26}2728// ์ค๋ฒ๋ผ์ด๋ฉ overriding29eat() {30super.eat() // ๋ถ๋ชจ์ ๊ธฐ๋ฅ์ ๊ทธ๋๋ก ์ ์งํ๋ฉด์ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ํ๊ณ ์ถ์ ๋31console.log('๊ฐ์์ง๊ฐ ๋จน๋๋ค!')32}33}3435const dog = new Dog('๋นจ๊ฐ์ด', '์๋ฆฌ')36console.log(dog) // Dog { color: '๋นจ๊ฐ์ด', ownerName: '์๋ฆฌ' }37dog.sleep() // ์๋ค38dog.eat() // ๋จน์! ๊ฐ์์ง๊ฐ ๋จน๋๋ค!39dog.play() // ๋์์~!