[TIL] Day22 #OOP #Class #Instance #Prototype

Beanxxยท2022๋…„ 5์›” 25์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
22/120
post-thumbnail

โœ๐Ÿป 2022.05.25.Wednesday
[TIL] Day22
[SEB FE] Day22

1๏ธโƒฃย ๊ฐ์ฒด ์ง€ํ–ฅ

๐Ÿ“Žย ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(OOP, Object-oriented programming)

  • ํ”„๋กœ๊ทธ๋žจ ์„ค๊ณ„ ์ฒ ํ•™
  • OOP์˜ ๋ชจ๋“  ๊ฒƒ์€ โ€˜๊ฐ์ฒด'๋กœ ๊ทธ๋ฃนํ™”
  • ์ด ๊ฐ์ฒด๋Š” ํ•œ ๋ฒˆ ๋งŒ๋“ค๊ณ  ๋‚˜๋ฉด, ๋ฉ”๋ชจ๋ฆฌ์ƒ์—์„œ ๋ฐ˜ํ™˜๋˜๊ธฐ ์ „๊นŒ์ง€ ๊ฐ์ฒด ๋‚ด์˜ ๋ชจ๋“  ๊ฒƒ์ด ์œ ์ง€
  • ์žฌ์‚ฌ์šฉ์„ฑ์„ ์–ป์„ ์ˆ˜ ์žˆ์Œ
  • ์‚ฌ๋žŒ์ด โ€˜์„ธ๊ณ„๋ฅผ ๋ณด๊ณ  ์ดํ•ดํ•˜๋Š” ๋ฐฉ๋ฒ•โ€™์„ ํ‰๋‚ด ๋‚ธ ๋ฐฉ๋ฒ•๋ก 

  • ํ•˜๋‚˜์˜ ๋ชจ๋ธ์ด ๋˜๋Š” ์ฒญ์‚ฌ์ง„ ๐Ÿ‘‰ย class
  • ๊ทธ ์ฒญ์‚ฌ์ง„์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ ๊ฐ์ฒด ๐Ÿ‘‰ย instance

โ†”๏ธย โžฐย ์ ˆ์ฐจ์  ์–ธ์–ด

  • ์ดˆ๊ธฐ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด (ex. C, ํฌํŠธ๋ž€)
  • ์ˆœ์ฐจ์ ์ธ ๋ช…๋ น์˜ ์กฐํ•ฉ

  • โœ‹ย ์ ˆ์ฐจ์  ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์ฝ”๋“œ ํ๋ฆ„์— ํฐ ์˜ํ–ฅ์„ ๋ฏธ์ณ ์œ ์ง€๋ณด์ˆ˜ ์–ด๋ ต

๐Ÿ“Žย OOP ์ฃผ์š” ๊ฐœ๋…

1. ์บก์Šํ™”(Encapsulation)

  • ๋ฐ์ดํ„ฐ(์†์„ฑ) & ๊ธฐ๋Šฅ(๋ฉ”์„œ๋“œ)์„ ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์•ˆ์— ๋„ฃ์–ด์„œ ๋ฌถ๋Š” ๊ฒƒ
  • ๋Š์Šจํ•œ ๊ฒฐํ•ฉ(Loose Coupling)์— ์œ ๋ฆฌ: ์–ธ์ œ๋“  ๊ตฌํ˜„ ์ˆ˜์ • ๊ฐ€๋Šฅ
    • ์ฝ”๋“œ ์‹คํ–‰ ์ˆœ์„œ์— ๋”ฐ๋ผ ์ ˆ์ฐจ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ฝ”๋“œ๊ฐ€ ์ƒ์ง•ํ•˜๋Š” ์‹ค์ œ ๋ชจ์Šต๊ณผ ๋‹ฎ๊ฒŒ ์ฝ”๋“œ๋ฅผ ๋ชจ์•„ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒƒ
  • ์€๋‹‰ํ™”(hiding): ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ/๊ตฌํ˜„์ด ์™ธ๋ถ€๋กœ ๋…ธ์ถœ๋˜์ง€ ์•Š๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ
    โ‡’ ๊ตฌํ˜„์€ ์ˆจ๊ธฐ๊ณ , ๋™์ž‘(๋ฉ”์„œ๋“œ)์€ ๋…ธ์ถœ
  • ๐Ÿ‘ย ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜์ง€ ์•Š๊ฒŒ ๋งŒ๋“ค๊ณ , ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ž„

2. ์ถ”์ƒํ™”(Abstraction)

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

โœ…ย ์บก์Šํ™” ๐Ÿ‘‰ย ์ฝ”๋“œ๋‚˜ ๋ฐ์ดํ„ฐ์˜ ์€๋‹‰์— ํฌ์ปค์Šค
โœ…ย ์ถ”์ƒํ™” ๐Ÿ‘‰ย ํด๋ž˜์Šค ์‚ฌ์šฉ์ž๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋ฉ”์„œ๋“œ๋ฅผ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ณ , ๋‹จ์ˆœํ•œ ์ด๋ฆ„์œผ๋กœ ์ •์˜ํ•˜๋Š” ๊ฒƒ์— ํฌ์ปค์Šค

3. ์ƒ์†(Inheritance)

  • ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ํŠน์ง•์„ ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ๋ฌผ๋ ค๋ฐ›๋Š” ๊ฒƒ
  • โ€˜๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ํŠน์ง•์„ ํŒŒ์ƒ ํด๋ž˜์Šค๊ฐ€ ์ƒ์†๋ฐ›๋Š”๋‹คโ€™
  • ๐Ÿ‘ย ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ๋ฅผ ์ค„์—ฌ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ž„

4. ๋‹คํ˜•์„ฑ(Polymorphism)

  • poly;๋งŽ์€ + morph;ํ˜•ํƒœ โ‡’ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ
  • ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ฉ”์„œ๋“œ๋ผ๋„ ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™
  • ๋งŒ์•ฝ ๋‹คํ˜•์„ฑ์„ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์ข…๋ฅ˜๋ณ„๋กœ ๋ถ„๊ธฐ๋ฅผ ์‹œ์ผœ์„œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค๋ฅด๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•จ
  • ๐Ÿ‘ย ๋™์ผํ•œ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์กฐ๊ฑด๋ฌธ ๋Œ€์‹  ๊ฐ์ฒด์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ๋‹ฌ๋ฆฌ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅ

โœ‹ย JS์—์„œ๋Š” ์€๋‹‰ํ™”์˜ ํ•œ๊ณ„(private ํ‚ค์›Œ๋“œ๋Š” ์—†์ง€๋งŒ, # ํ‚ค์›Œ๋“œ ๋„์ž…) & ์ถ”์ƒํ™”(interface) ๊ธฐ๋Šฅ์˜ ๋ถ€์žฌ ์กด์žฌ


โžฐย ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
๊ฐ์ฒด.๋ฉ”์„œ๋“œ() ํ˜•์‹์œผ๋กœ ๊ฐ์ฒด ๋‚ด์— ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

โœ‹ย ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ โŒ

โžฐย ํด๋กœ์ € ๋ชจ๋“ˆ ํŒจํ„ด ์ด์šฉ โ†’ ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์ƒ์„ฑ

function makeCounter() {
	let value = 0;
	return {
		increase: function() {
			value++;
		},
		decrease: function() {
			value--;
		},
		getValue: function() {
			return value;
		}
	}
}

let counter1 = makeCounter()
counter1.increase()
counter1.getValue() // 1

let counter2 = makeCounter()
counter2.decrease()
counter2.decrease()
counter2.getValue() // -2

โ˜‘๏ธย Class

โžฐย ์†์„ฑ & ๋ฉ”์†Œ๋“œ

  • ํด๋ž˜์Šค์— ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ธ์Šคํ„ด์Šค์—์„œ ์ด์šฉ

๐Ÿ“Žย ES5: Class ์ž‘์„ฑ ๋ฌธ๋ฒ•

  • ํ•จ์ˆ˜๋กœ ์ •์˜
  • ํด๋ž˜์Šค ์ด๋ฆ„์€ ๋ณดํ†ต ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘, ์ผ๋ฐ˜๋ช…์‚ฌ๋กœ ์ž‘์„ฑ
  • prototype ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ๋ฉ”์„œ๋“œ ์ •์˜ ๊ฐ€๋Šฅ
function Car(brand, name, color) {
	// instance ์ƒ์„ฑ์‹œ ์‹คํ–‰๋˜๋Š” code
	this.brand = brand;
	this.name = name;
	this.color = color;
}

// ๋ฉ”์†Œ๋“œ ์ •์˜
Car.prototype.refuel = function() {
	console.log('์—ฐ๋ฃŒ ๊ณต๊ธ‰!!');
}

Car.prototype.drive = function() {
	console.log('์šด์ „ ์‹œ์ž‘!!');
}

๐Ÿ“Žย ES6: Class ์ž‘์„ฑ ๋ฌธ๋ฒ•

  • class ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •์˜
  • ์ƒ์„ฑ์ž(constructor) ํ•จ์ˆ˜ ์กด์žฌ (instance ์ƒ์„ฑ์‹œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ)
class Car {
	// ์†์„ฑ ์ •์˜
	// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
	constructor(brand, name, color) {
		// instance ์ƒ์„ฑ์‹œ ์‹คํ–‰๋˜๋Š” code
		this.brand = brand; // ๋งŒ๋“ค์–ด์ง„ ์ธ์Šคํ„ด์Šค์— ํ•ด๋‹น ๋ธŒ๋žœ๋“œ๋ฅผ ๋ถ€์—ฌ
		this.name = name;
		this.color = color;
	}

	// ๋ฉ”์†Œ๋“œ ์ •์˜
	refuel() {
		console.log('์—ฐ๋ฃŒ ๊ณต๊ธ‰!!');
	}

	drive() {
		console.log('์šด์ „ ์‹œ์ž‘!!');
	}
}

โœ‹ย this keyword: ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด ์˜๋ฏธ


โ˜‘๏ธย Instance

  • new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ
  • ๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค๋Š” Car๋ผ๋Š” ํด๋ž˜์Šค์˜ ๊ณ ์œ ํ•œ ์†์„ฑ&๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง
// ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
let avanate = new Car('hyundai', 'avante', 'black');

// ์ธ์Šคํ„ด์Šค์—์„œ์˜ ์‚ฌ์šฉ
avante.color;   // 'black'
avante.brand;   // 'hyundai'
avante.drive(); // '์šด์ „ ์‹œ์ž‘!!'

// ๋ฐฐ์—ด์€ Array์˜ instance

// ๋ฐฐ์—ด ์ •์˜ = Array์˜ instance ์ƒ์„ฑ
let arr = new Array('software', 'engineering');
arr.length; // 2
arr.push('bootcamp'); // new element ์ถ”๊ฐ€

๐Ÿคทโ€โ™€๏ธย Array.prototype.push()์—์„œ prototype์ด ๋ถ™์–ด์žˆ๋Š” ์ด์œ ๋Š”?
ย ย ย ย ย  ๐Ÿ‘‰ย push ๊ตฌํ˜„์ด ์›ํ˜• ๊ฐ์ฒด์— ์ •์˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ


prototypeconstructorthis
๋ชจ๋ธ์˜ ์ฒญ์‚ฌ์ง„์„ ๋งŒ๋“ค ๋•Œ ์“ฐ๋Š” ์›ํ˜• ๊ฐ์ฒด(original form)์ธ์Šคํ„ด์Šค๊ฐ€ ์ดˆ๊ธฐํ™”๋  ๋•Œ ์‹คํ–‰ํ•˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜- ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ๋•Œ, ํ•ด๋‹น ์Šค์ฝ”ํ”„๋งˆ๋‹ค ์ƒ์„ฑ๋˜๋Š” ๊ณ ์œ ํ•œ ์‹คํ–‰ context
- new ํ‚ค์›Œ๋“œ๋กœ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์‹œ, ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”๋กœ this์˜ ๊ฐ’
  • ํด๋ž˜์Šค: ์ผ์ข…์˜ ์›ํ˜•(original form)์œผ๋กœ, ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์•„์ด๋””์–ด/์ฒญ์‚ฌ์ง„ (์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํฌํ•จ)
  • ์ธ์Šคํ„ด์Šค: ํด๋ž˜์Šค์˜ ์‚ฌ๋ก€(instance object) / ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด

โœ‹ย ํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์„ธ๋ถ€ ์‚ฌํ•ญ(์†์„ฑ)์„ ๋„ฃ์œผ๋ฉด ๊ฐ์ฒด๊ฐ€ ๋จ


2๏ธโƒฃย Prototype

  • JS๋Š” Prototype(์›ํ˜• ๊ฐ์ฒด) ๊ธฐ๋ฐ˜ ์–ธ์–ด
    ๐Ÿ‘‰ย ๋ชจ๋“  ๊ฐ์ฒด๋“ค์ด ๋ฉ”์†Œ๋“œ์™€ ์†์„ฑ๋“ค์„ ์ƒ์† ๋ฐ›๊ธฐ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ์œผ๋กœ์จ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ์˜๋ฏธ
  • โžฐย ์ƒ์†๋˜๋Š” ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๋“ค์€ ๊ฐ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž์˜ prototype ์†์„ฑ์— ์ •์˜๋˜์–ด ์žˆ์Œ

โžฐย Prototype ํ•ด์„
1. ํ•จ์ˆ˜์˜ ๋ฉค๋ฒ„์ธ prototype ์†์„ฑ์€ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์†์„ฑ
2. ํ•จ์ˆ˜์™€ new ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ์—ญํ• 


๐Ÿ“Žย .proto

  • ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ ์›ํ˜•์ธ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์ˆจ๊ฒจ์ง„ ๋งํฌ๋กœ์จ ํ”„๋กœํ† ํƒ€์ž…์„ ์˜๋ฏธ
  • ๋งŽ์€ ๋ธŒ๋ผ์šฐ์ €๋“ค์ด ์ƒ์„ฑ์ž์˜ prototype ์†์„ฑ์—์„œ ํŒŒ์ƒ๋œ __proto__ ์†์„ฑ์œผ๋กœ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค์— ๊ตฌํ˜„

โœ‹ย Array๋Š” ํด๋ž˜์Šค - ๋ฐฐ์—ด์€ Array ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๋ฉฐ, Array.prototype์—๋Š” push, pop ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ ์กด์žฌ


class Human {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    sleep() {
        console.log(`${this.name}์€ ์ž ์— ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค`);
    }
}

let kimcoding = new Human('๊น€์ฝ”๋”ฉ', 30);

// Human ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜(constructor)๋Š” Human
Human.prototype.constructor === Human;     // true

// Human ํด๋ž˜์Šค์˜ prototype์€ Human ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ kimcoding์˜ __proto__
Human.prototype === kimcoding.__proto__;   // true

// Human ํด๋ž˜์Šค์˜ sleep ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœํ† ํƒ€์ž…์— ์žˆ์œผ๋ฉฐ,
// Human ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์˜ kimcoding์—์„œ kimcoding.sleep์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
Human.prototype.sleep === kimcoding.sleep; // true

๐Ÿ“Žย ํด๋ž˜์Šค, ์ธ์Šคํ„ด์Šค, ํ”„๋กœํ† ํƒ€์ž…์˜ ๊ด€๊ณ„

173ADF1A-F41C-4037-9A65-D4817A4B70F5_1_201_a

์•„๋ž˜ ์ฐธ๊ณ ํ–ˆ๋˜ ์‚ฌ์ดํŠธ๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์„ค๋ช…๋˜์–ด ์žˆ์–ด์„œ ์ฐธ๊ณ ํ•ด์„œ ๊ด€๊ณ„๋„?๋ฅผ ๊ทธ๋ ค๋ดค๋‹ค.

โœ”๏ธย ํ”„๋กœํ† ํƒ€์ž…์˜ ์–ฝํžŒ ๊ด€๊ณ„๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์ข‹์•˜๋˜ ์ฐธ๊ณ  ์‚ฌ์ดํŠธ
โœ”๏ธย ํ”„๋กœํ† ํƒ€์ž… ์‹ค์Šต ๊ฐ€์ด๋“œ ์‚ฌ์ดํŠธ

profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€