λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
IT

μžλ°”μŠ€ν¬λ¦½νŠΈ Prototype: κ°œλ…, ν™œμš©, μž₯단점, 예제 μ½”λ“œ

by πŸ’²πŸŽ΅βœ–οΈβœ”οΈβ˜Ό 2024. 2. 28.
728x90

μžλ°”μŠ€ν¬λ¦½νŠΈ Prototype: κ°œλ…, ν™œμš©, μž₯단점, 예제 μ½”λ“œ

Prototypeμ΄λž€?

μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” ν”„λ‘œν† νƒ€μž… 기반 μ–Έμ–΄λ‘œ, λͺ¨λ“  κ°μ²΄λŠ” λ‹€λ₯Έ κ°μ²΄λ‘œλΆ€ν„° μƒμ†λœ ν”„λ‘œν† νƒ€μž…μ„ 가지고 μžˆμŠ΅λ‹ˆλ‹€. 이 ν”„λ‘œν† νƒ€μž…μ€ ν•΄λ‹Ή 객체의 λΆ€λͺ¨ 역할을 ν•˜λ©°, 객체 간에 ν”„λ‘œν† νƒ€μž… 체인을 ν˜•μ„±ν•©λ‹ˆλ‹€. μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ λͺ¨λ“  κ°μ²΄λŠ” μžμ‹ μ˜ ν”„λ‘œν† νƒ€μž…μ„ κ°€λ¦¬ν‚€λŠ” __proto__λΌλŠ” λ‚΄λΆ€ ν”„λ‘œνΌν‹°λ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€.

μ—­ν• 

  1. 상속: 객체가 ν”„λ‘œν† νƒ€μž…μ„ 가지고 있으면, ν•΄λ‹Ή ν”„λ‘œν† νƒ€μž…μ˜ ν”„λ‘œνΌν‹°μ™€ λ©”μ†Œλ“œλ₯Ό 상속받아 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  2. λ©”μ†Œλ“œ 및 ν”„λ‘œνΌν‹° 곡유: μ—¬λŸ¬ 객체가 ν•˜λ‚˜μ˜ ν”„λ‘œν† νƒ€μž…μ„ κ³΅μœ ν•˜λ©΄, ν•΄λ‹Ή ν”„λ‘œν† νƒ€μž…μ— λŒ€ν•œ 변경이 λͺ¨λ“  객체에 영ν–₯을 λ―ΈμΉ©λ‹ˆλ‹€.

μ‚¬μš© 상황

1. 상속 κ΅¬ν˜„

function Animal(name) {
  this.name = name;
}

Animal.prototype.sayHello = function() {
  console.log(`Hello, I'm ${this.name}`);
};

function Dog(name, breed) {
  Animal.call(this, name);
  this.breed = breed;
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

const myDog = new Dog('Buddy', 'Golden Retriever');
myDog.sayHello(); // 좜λ ₯: Hello, I'm Buddy

2. ν”„λ‘œν† νƒ€μž… 체인

const myArray = [1, 2, 3];
console.log(myArray.toString()); // 배열이 Array.prototype을 상속받아 toString λ©”μ†Œλ“œ μ‚¬μš©

μž₯단점

μž₯점

  • λ©”λͺ¨λ¦¬ μ΅œμ ν™”: μ—¬λŸ¬ 객체가 ν•˜λ‚˜μ˜ ν”„λ‘œν† νƒ€μž…μ„ κ³΅μœ ν•˜λ―€λ‘œ, μ€‘λ³΅λœ λ©”μ†Œλ“œμ™€ ν”„λ‘œνΌν‹°λ₯Ό λ©”λͺ¨λ¦¬μ— ν•œ 번만 μ €μž₯ν•˜μ—¬ μžμ›μ„ 효율적으둜 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 동적 ν™•μž₯: 객체의 ν”„λ‘œν† νƒ€μž…μ„ λ™μ μœΌλ‘œ λ³€κ²½ν•˜μ—¬ μƒˆλ‘œμš΄ λ©”μ†Œλ“œλ‚˜ ν”„λ‘œνΌν‹°λ₯Ό μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

단점

  • 객체 λ³€κ²½ μ‹œ 주의: ν”„λ‘œν† νƒ€μž…μ„ λ³€κ²½ν•˜λ©΄ ν•΄λ‹Ή ν”„λ‘œν† νƒ€μž…μ„ μƒμ†λ°›λŠ” λͺ¨λ“  객체에 영ν–₯을 λ―ΈμΉ˜λ―€λ‘œ μ£Όμ˜κ°€ ν•„μš”ν•©λ‹ˆλ‹€.
  • 상속 κ΅¬ν˜„ λ³΅μž‘μ„±: ν”„λ‘œν† νƒ€μž…μ„ μ΄μš©ν•œ 상속 κ΅¬ν˜„μ€ λͺ…μ‹œμ μ΄μ§€ μ•Šμ•„ 처음 λ³΄λŠ” μ‚¬λžŒλ“€μ—κ²Œ ν˜Όλž€μ„ 쀄 수 μžˆμŠ΅λ‹ˆλ‹€.

예제 μ½”λ“œ

1. 상속 κ΅¬ν˜„

function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
};

function Circle(radius) {
  Shape.call(this);
  this.radius = radius;
}

Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.constructor = Circle;

const myCircle = new Circle(5);
myCircle.move(1, 2);
console.log(myCircle.x, myCircle.y); // 좜λ ₯: 1, 2

2. ν”„λ‘œν† νƒ€μž… 체인 ν™œμš©

function Person(name) {
  this.name = name;
}

Person.prototype.sayHello = function() {
  console.log(`Hello, I'm ${this.name}`);
};

const john = new Person('John');
john.sayHello(); // 좜λ ₯: Hello, I'm John

μ—°κ΄€λœ 기술

ES6 Class 문법

ES6λΆ€ν„° λ„μž…λœ Class 문법은 ν”„λ‘œν† νƒ€μž… 기반의 상속을 보닀 λͺ…μ‹œμ μ΄κ³  κ°„νŽΈν•˜κ²Œ κ΅¬ν˜„ν•  수 있게 ν•΄μ£ΌλŠ” λ¬Έλ²•μž…λ‹ˆλ‹€. ν•˜μ§€λ§Œ λ‚΄λΆ€μ μœΌλ‘œλŠ” μ—¬μ „νžˆ ν”„λ‘œν† νƒ€μž…μ„ ν™œμš©ν•©λ‹ˆλ‹€.

class

 Animal {
  constructor(name) {
    this.name = name;
  }

  sayHello() {
    console.log(`Hello, I'm ${this.name}`);
  }
}

class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }
}

const myDog = new Dog('Buddy', 'Golden Retriever');
myDog.sayHello(); // 좜λ ₯: Hello, I'm Buddy

μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ ν”„λ‘œν† νƒ€μž…μ€ μ½”λ“œμ˜ μž¬μ‚¬μš©μ„±κ³Ό λ©”λͺ¨λ¦¬ μ΅œμ ν™”μ— κΈ°μ—¬ν•˜λŠ” μ€‘μš”ν•œ μš”μ†Œμž…λ‹ˆλ‹€. μ μ ˆν•œ μƒν™©μ—μ„œ ν™œμš©ν•¨μœΌλ‘œμ¨ μ½”λ“œμ˜ 가독성과 μœ μ§€λ³΄μˆ˜μ„±μ„ ν–₯μƒμ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

λŒ“κΈ€