๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Lang
JavaScript
16-Class

1. ํด๋ž˜์Šค(class)

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” โ€˜๋ถ•์–ด๋นต ๊ธฐ๊ณ„โ€™์ฒ˜๋Ÿผ ๊ฐ์ฒด๋ฅผ ์ฐ์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” โ€˜ํ…œํ”Œ๋ฆฟโ€™ ์–‘์‹ ๊ฐ™์€ ๊ฐœ๋…์ด๊ณ ,
  • ๊ฐ์ฒด๋Š” ํ…œํ”Œ๋ฆฟ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ์ž…ํ•ด์„œ ๋งŒ๋“  โ€˜๋ถ•์–ด๋นตโ€™์— ๋น„์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” JavaScript์—์„œ๋Š” ํ”„๋กœํ† ํƒ€์ž…(Prototype)์„ ํ† ๋Œ€๋กœ ํ•ด์„œ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ง€์›ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋Œ€๋‹ค์ˆ˜ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด๋“ค์€ ํ”„๋กœํ† ํƒ€์ž…๋ณด๋‹ค ํด๋ž˜์Šค(Class)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. JavaScript์˜ ๋‚ด๋ถ€์—์„œ๋Š” ํ”„๋กœํ† ํƒ€์ž…์„ ์“ฐ๊ณ , ๊ฐœ๋ฐœ์ž๋“ค์€ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด๋„ ๋‚ด๋ถ€์—์„œ๋Š” ํ”„๋กœํ† ํƒ€์ž…์„ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ ES6+ ์ด์ƒ์˜ JavaScript์—์„œ Class ๋ฌธ๋ฒ•์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ˜„์—…์˜ ๋Œ€๋ถ€๋ถ„ ์–ธ์–ด๋Š” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๐Ÿ’ก ์šฉ์–ด ์ •๋ฆฌ

  • ํด๋ž˜์Šค(Class) : ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํ…œํ”Œ๋ฆฟ (e.g. ์ฒญ์‚ฌ์ง„, ํ‹€, ์–‘์‹, ์„ค๊ณ„๋„)
    • ํŠน์ • ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ(ํ•จ์ˆ˜)๋ฅผ ์ •์˜ํ•˜๋Š” ํ‹€
  • ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object-Oriented Programming) : ๊ฐ์ฒด๋ผ๋ฆฌ ์„œ๋กœ ํ˜ธํ™˜๋˜๋„๋ก ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” ๊ฒƒ
  • ์ธ์Šคํ„ด์Šค(Instance) : ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋–„ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋“ค
1
// ๊ฐ์ฒด๋ฅผ ์†์‰ฝ๊ฒŒ ๋งŒ๋“ค์ˆ˜ ์žˆ๋Š” ํ…œํ”Œ๋ฆฟ
2
// 1. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ (์˜ค๋ž˜๋œ ๊ณ ์ „์ ์ธ ๋ฐฉ๋ฒ•)
3
// 2. ํด๋ž˜์Šค โœจ
4
class Fruit {
5
// ๋ชจ๋“  ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž(constructor)๊ฐ€ ์กด์žฌํ•ด์•ผ ํ•จ
6
// ์ƒ์„ฑ์ž: new ํ‚ค์›Œ๋“œ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
7
constructor(name, emoji) {
8
this.name = name
9
this.emoji = emoji
10
}
11
12
display = () => {
13
console.log(`${this.name}: ${this.emoji}`)
14
}
15
}
16
17
const apple = new Fruit('apple', '๐ŸŽ') // apple์€ Fruit ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๋‹ค.
18
const orange = new Fruit('orange', '๐ŸŠ') // orange์€ Fruit ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๋‹ค.
19
20
console.log(apple) // Fruit { display: [Function: display], name: 'apple', emoji: '๐ŸŽ' }
21
console.log(orange) // Fruit { display: [Function: display], name: 'orange', emoji: '๐ŸŠ' }
22
console.log(apple.name)
23
console.log(apple.emoji)
24
apple.display()
25
26
// obj๋Š” ๊ฐ์ฒด์ด๊ณ , ๊ทธ ์–ด๋–ค ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋„ ์•„๋‹ˆ๋‹ค.
27
const obj = { name: 'ellie' }

cf. MDN Class


2. ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•(static)

ํด๋ž˜์Šค์— ์ •์˜๋œ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ๋ ˆ๋ฒจ์˜ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ํด๋ž˜์Šค์—์„œ ์ •์˜ํ•œ ํ”„๋กœํผํ‹ฐ์™€ ํ•จ์ˆ˜๋“ค์ด ์ธ์Šคํ„ด์Šค์— ์ค‘๋ณต์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค๋“ค์„ ํ†ตํ•ด ๊ฐ์ฒด.์†์„ฑ ๋˜๋Š” ๊ฐ์ฒด.ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ฐ์ฒด๋งˆ๋‹ค ๋™์ผํ•˜๊ฒŒ ์ฐธ์กฐํ•ด์•ผ ํ•˜๋Š” ์†์„ฑ์ด๋‚˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด, ํด๋ž˜์Šค ๋ ˆ๋ฒจ์˜ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํด๋ž˜์Šค ์•ˆ์— static ํ‚ค์›Œ๋“œ๋ฅผ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ ์•ž์— ์ž‘์„ฑํ•˜๋ฉด, ๋งŒ๋“ค์–ด์ง„ ์ธ์Šคํ„ด์Šค์— ํฌํ•จ๋˜์ง€ ์•Š๊ณ  ํด๋ž˜์Šค์— ๊ทธ๋Œ€๋กœ ๋‚จ์•„์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํด๋ž˜์Šค์— 1๋ฒˆ๋งŒ ์ •์˜ํ•˜๊ณ  ์ธ์Šคํ„ด์Šค์—์„œ ์žฌ์‚ฌ์šฉ์„ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ˜ธ์ถœํ•  ๋–„๋„๋งŒ๋“ค์–ด์ง„ ์ธ์Šคํ„ด์Šค.์ด ์•„๋‹ˆ๋ผ ํด๋ž˜์Šค๋ช….์œผ๋กœ ํด๋ž˜์Šค ๋ ˆ๋ฒจ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1
// static ์ •์  ํ”„๋กœํผํ‹ฐ, ๋ฉ”์„œ๋“œ
2
class Fruit {
3
static MAX_FRUITS = 4
4
// ์ƒ์„ฑ์ž: new ํ‚ค์›Œ๋“œ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
5
constructor(name, emoji) {
6
this.name = name
7
this.emoji = emoji
8
}
9
10
// ํด๋ž˜์Šค ๋ ˆ๋ฒจ์˜ ๋ฉ”์„œ๋“œ
11
static makeRandomFruit() {
12
// ํด๋ž˜์Šค ๋ ˆ๋ฒจ์˜ ๋ฉ”์„œ๋“œ์—์„œ๋Š” this๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์—†์Œ
13
return new Fruit('banana', '๐ŸŒ')
14
}
15
16
// ์ธ์Šคํ„ด์Šค ๋ ˆ๋ฒจ์˜ ๋ฉ”์„œ๋“œ
17
display = () => {
18
console.log(`${this.name}: ${this.emoji}`)
19
}
20
}
21
22
const banana = Fruit.makeRandomFruit()
23
console.log(banana) // Fruit { display: [Function: display], name: 'banana', emoji: '๐ŸŒ' }
24
console.log(Fruit.MAX_FRUITS) // 4
25
26
// apple์€ Fruit ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๋‹ค.
27
const apple = new Fruit('apple', '๐ŸŽ')
28
// orange์€ Fruit ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๋‹ค.
29
const orange = new Fruit('orange', '๐ŸŠ')
30
31
console.log(apple) // Fruit { display: [Function: display], name: 'apple', emoji: '๐ŸŽ' }
32
console.log(orange) // Fruit { display: [Function: display], name: 'orange', emoji: '๐ŸŠ' }
33
console.log(apple.name)
34
console.log(apple.emoji)
35
apple.display()
36
37
Math.pow() // math ํด๋ž˜์Šค API
38
Number.isFinite(1) // number ํด๋ž˜์Šค API

3. ํ•„๋“œ(field)

1
// ์ ‘๊ทผ์ œ์–ด์ž - ์บก์Аํ™”
2
// private(#), public(๊ธฐ๋ณธ), protected
3
class Fruit {
4
// # : ๋‚ด๋ถ€์—์„œ๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅ, ์™ธ๋ถ€์—์„œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€
5
#name
6
#emoji
7
#type = '๊ณผ์ผ'
8
constructor(name, emoji) {
9
this.#name = name
10
this.#emoji = emoji
11
}
12
#display = () => {
13
console.log(`${this.#name}: ${this.#emoji}`)
14
}
15
}
16
17
const apple = new Fruit('apple', '๐ŸŽ')
18
// apple.#name = '์˜ค๋ Œ์ง€'; // #field๋Š” ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•จ
19
console.log(apple) // Fruit {}

4. getter์™€ setter

1
// ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ (Accessor Property)
2
class Student {
3
constructor(firstName, lastName) {
4
this.firstName = firstName
5
this.lastName = lastName
6
}
7
8
/*** Getter์˜ ์—ญํ• 
9
* 1) ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ• ๋–„
10
* 2) privateํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋–„
11
*/
12
// fullName์— ์ ‘๊ทผ(ํ˜ธ์ถœ)ํ•  ๋–„
13
get fullName() {
14
return `${this.lastName} ${this.firstName}`
15
}
16
17
/*** Setter์˜ ์—ญํ• 
18
* ํ”„๋กœํผํ‹ฐ๋ฅผ priavte๋กœ ๋งŒ๋“ค๋ฉด ์ง์ ‘ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
19
* ์ด๋Ÿด ๋–„, Setter๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
20
* Setter๋Š” ๋งŽ์ด ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.
21
*/
22
// fullName์— ํ• ๋‹นํ•  ๋–„
23
set fullName(value) {
24
console.log('set', value)
25
}
26
}
27
28
const student = new Student('์ˆ˜์ง€', '๊น€')
29
student.firstName = '์•ˆ๋‚˜'
30
console.log(student.firstName) // ์•ˆ๋‚˜
31
console.log(student.fullName) // ๊น€ ์•ˆ๋‚˜
32
student.fullName = '๊น€์ฒ ์ˆ˜' // set ๊น€์ฒ ์ˆ˜

5. ์ƒ์†(inheritance)-super

  • super ํ‚ค์›Œ๋“œ : ๋ถ€๋ชจ์˜ ๊ธฐ๋Šฅ์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ํ•˜๊ณ  ์‹ถ์„ ๋–„
1
class Animal {
2
constructor(color) {
3
this.color = color
4
}
5
eat() {
6
console.log('๋จน์ž!')
7
}
8
sleep() {
9
console.log('์ž”๋‹ค')
10
}
11
}
12
13
class Tiger extends Animal {}
14
const tiger = new Tiger('๋…ธ๋ž‘์ด')
15
console.log(tiger) // Tiger { color: '๋…ธ๋ž‘์ด' }
16
tiger.sleep() // ์ž”๋‹ค
17
tiger.eat() // ๋จน์ž!
18
19
class Dog extends Animal {
20
constructor(color, ownerName) {
21
super(color)
22
this.ownerName = ownerName
23
}
24
play() {
25
console.log('๋†€์ž์•„~!')
26
}
27
28
// ์˜ค๋ฒ„๋ผ์ด๋”ฉ overriding
29
eat() {
30
super.eat() // ๋ถ€๋ชจ์˜ ๊ธฐ๋Šฅ์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ํ•˜๊ณ  ์‹ถ์„ ๋–„
31
console.log('๊ฐ•์•„์ง€๊ฐ€ ๋จน๋Š”๋‹ค!')
32
}
33
}
34
35
const dog = new Dog('๋นจ๊ฐ•์ด', '์—˜๋ฆฌ')
36
console.log(dog) // Dog { color: '๋นจ๊ฐ•์ด', ownerName: '์—˜๋ฆฌ' }
37
dog.sleep() // ์ž”๋‹ค
38
dog.eat() // ๋จน์ž! ๊ฐ•์•„์ง€๊ฐ€ ๋จน๋Š”๋‹ค!
39
dog.play() // ๋†€์ž์•„~!