μλ°μ€ν¬λ¦½νΈ Prototype: κ°λ , νμ©, μ₯λ¨μ , μμ μ½λ
Prototypeμ΄λ?
μλ°μ€ν¬λ¦½νΈλ νλ‘ν νμ
κΈ°λ° μΈμ΄λ‘, λͺ¨λ κ°μ²΄λ λ€λ₯Έ κ°μ²΄λ‘λΆν° μμλ νλ‘ν νμ
μ κ°μ§κ³ μμ΅λλ€. μ΄ νλ‘ν νμ
μ ν΄λΉ κ°μ²΄μ λΆλͺ¨ μν μ νλ©°, κ°μ²΄ κ°μ νλ‘ν νμ
체μΈμ νμ±ν©λλ€. μλ°μ€ν¬λ¦½νΈμ λͺ¨λ κ°μ²΄λ μμ μ νλ‘ν νμ
μ κ°λ¦¬ν€λ __proto__
λΌλ λ΄λΆ νλ‘νΌν°λ₯Ό κ°μ§κ³ μμ΅λλ€.
μν
- μμ: κ°μ²΄κ° νλ‘ν νμ μ κ°μ§κ³ μμΌλ©΄, ν΄λΉ νλ‘ν νμ μ νλ‘νΌν°μ λ©μλλ₯Ό μμλ°μ μ¬μ©ν μ μμ΅λλ€.
- λ©μλ λ° νλ‘νΌν° 곡μ : μ¬λ¬ κ°μ²΄κ° νλμ νλ‘ν νμ μ 곡μ νλ©΄, ν΄λΉ νλ‘ν νμ μ λν λ³κ²½μ΄ λͺ¨λ κ°μ²΄μ μν₯μ λ―ΈμΉ©λλ€.
μ¬μ© μν©
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
μλ°μ€ν¬λ¦½νΈμ νλ‘ν νμ μ μ½λμ μ¬μ¬μ©μ±κ³Ό λ©λͺ¨λ¦¬ μ΅μ νμ κΈ°μ¬νλ μ€μν μμμ λλ€. μ μ ν μν©μμ νμ©ν¨μΌλ‘μ¨ μ½λμ κ°λ μ±κ³Ό μ μ§λ³΄μμ±μ ν₯μμν¬ μ μμ΅λλ€.
'IT' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
JVM(Java Virtual Machine)μ μ΄ν΄μ νμ© (0) | 2024.03.01 |
---|---|
BFF(Backend For Frontend) κ°λ λ° νμ© (0) | 2024.03.01 |
μλ°μ€ν¬λ¦½νΈμ this: κ°λ , νμ©, μ₯λ¨μ , μμ μ½λ (0) | 2024.02.27 |
SSL/TLS: μνΈνμ 보μμ μν ν΅μ¬ νλ‘ν μ½ (0) | 2024.02.27 |
μ»΄ν¨ν° κ³Όν(CS)μ ν΅μ¬ μ΄ν΄: κΈ°μ΄λΆν° μ¬νκΉμ§ (0) | 2024.02.27 |
λκΈ