νλ‘ν νμ
1.
μ νλ‘ν νμ
μ λ°°μ°λκ°
2.
νλ‘ν νμ
μ μ
3.
νλ‘ν νμ
κ³Ό κ°μ²΄ μμ±
4.
νλ‘ν νμ
체μΈ
5.
νλ‘ν νμ
μμ
6.
ES6 classμ νλ‘ν νμ
7.
μ 체 μμ½
μλ°μ€ν¬λ¦½νΈ νλ‘ν νμ
μ νλ‘ν νμ μ λ°°μ°λκ°?
μλ°μ€ν¬λ¦½νΈλ C++, Javaμ²λΌ βν΄λμ€ κΈ°λ°β μΈμ΄κ° μλλΌ, νλ‘ν νμ
κΈ°λ° μΈμ΄μ΄λ€.
μ¦, κ°μ²΄κ° μ΄λ€ ν(Class)λ‘λΆν° μμ±λλ κ²μ΄ μλλΌ, λ€λ₯Έ κ°μ²΄λ₯Ό μμ μ λΆλͺ¨μ²λΌ μΌλ λ°©μμΌλ‘ κΈ°λ₯μ μμνλ€.
ES6μμ class λ¬Έλ²μ΄ μκΈ°κΈ΄ νμ§λ§, λ΄λΆμ μΌλ‘λ μ¬μ ν νλ‘ν νμ
κΈ°λ° κ΅¬μ‘°λ‘ μλνλ€.
λ°λΌμ νλ‘ν νμ
μ μ΄ν΄νλ©΄:
β’
κ°μ²΄κ° μ΄λ»κ² κΈ°λ₯μ 곡μ νλμ§
β’
λ©λͺ¨λ¦¬κ° μ΄λ»κ² μ μ½λλμ§
β’
class λ¬Έλ²μ΄ μ€μ λ‘ λ΄λΆμμ 무μμ νλμ§
λ₯Ό μ ννκ² μ΄ν΄ν μ μλ€.
νλ‘ν νμ μ μ
βκ°μ²΄μ μνβ
νλ‘ν νμ
(Prototype)μ κ°μ²΄λ€μ΄ 곡ν΅μ μΌλ‘ μ¬μ©νλ μν(λΆλͺ¨ κ°μ²΄)μ μλ―Ένλ€.
μλ°μ€ν¬λ¦½νΈμμ κ°μ²΄λ μμ±λ λ μλμΌλ‘ νΉμ νλ‘ν νμ
κ³Ό μ°κ²°λλ€.
μ΄ νλ‘ν νμ
μλ κ³΅ν΅ λ©μλλ μμ±μ μ μ₯ν΄λκ³ , λͺ¨λ κ°μ²΄λ μ΄λ₯Ό 곡μ νλ€.
μμ
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function () {
console.log(`μλ
νμΈμ, μ λ ${this.name}μ
λλ€.`);
};
const p1 = new Person("νκΈΈλ");
p1.sayHello();
JavaScript
볡μ¬
μ€λͺ
β’
Person.prototype μ βPerson κ°μ²΄μ μνβ
β’
sayHello λ λͺ¨λ Person μΈμ€ν΄μ€κ° 곡μ
β’
p1 κ°μ²΄μ sayHelloκ° μμ§λ§, λΆλͺ¨μΈ Person.prototypeμμ μ°Ύμ μ€ννλ€
νλ‘ν νμ
μ λ§μΉ βμ¬λ¬ κ°μ²΄κ° ν¨κ» μ¬μ©νλ κ³΅ν΅ κ΅κ³Όμβμ κ°λ€.
νλ‘ν νμ κ³Ό κ°μ²΄ μμ±
κ°μ²΄λ₯Ό μμ±ν λ νλ‘ν νμ
μ μλμΌλ‘ μ°κ²°λλ€.
νΉν μμ±μ ν¨μμ new μ°μ°μλ‘ μμ±ν λ λ€μ κ³Όμ μ κ±°μΉλ€.
μμ
function Person(name) {
this.name = name;
}
const p1 = new Person("μ² μ");
JavaScript
볡μ¬
λ΄λΆ λμ μ€λͺ
new Person("μ² μ") λ μλ μμλ₯Ό κ±°μΉλ€.
1.
μλ‘μ΄ λΉ κ°μ²΄ {} μμ±
2.
μ κ°μ²΄μ __proto__ κ° Person.prototype μΌλ‘ μ°κ²°
3.
Person ν¨μ μ€ν, thisμ νλ‘νΌν° μ μ₯
4.
μμ±λ κ°μ²΄ λ°ν
μ¦ λ€μμ΄ νμ μ±λ¦½νλ€:
p1.__proto__ === Person.prototype // true
JavaScript
볡μ¬
μ΄λ κ² κ°μ²΄λ μμ μ΄ μ¬μ©ν "λΆλͺ¨" μν μ νλ‘ν νμ
κ³Ό μ°κ²°λλ€.
νλ‘ν νμ 체μΈ
κ°μ²΄μμ μμ±μ΄λ λ©μλλ₯Ό μ¬μ©νλ €κ³ ν λ, μλ°μ€ν¬λ¦½νΈλ λ€μ μμλ‘ νμνλ€.
1.
obj μ체μμ methodλ₯Ό μ°Ύκ³
2.
μμΌλ©΄ objμ __proto__ (νλ‘ν νμ
)μμ μ°Ύκ³
3.
κ·Έλλ μμΌλ©΄ κ·Έ μμ νλ‘ν νμ
μμ κ³μ νμν©λλ€.
μμ
const obj = { a: 1 };
console.log(obj.toString());
JavaScript
볡μ¬
μ€λͺ
β’
objμλ toStringμ΄ μλ€
β’
κ·Έλμ obj.proto β Object.prototype μμ toStringμ μ°Ύλλ€
β’
Object.prototypeλ μλ€λ©΄ nullκΉμ§ νμ
β’
nullμμ λλλλ‘ μ€κ³λμ΄ μλ€
μ¦, 체μΈμ λ€μκ³Ό κ°λ€:
obj β Object.prototype β null
Plain Text
볡μ¬
νλ‘ν νμ μμ
νλ‘ν νμ
μ μ΄μ©νλ©΄ κ°μ²΄ κ° μμλ κ°λ₯νλ€.
νμ κ°μ²΄κ° μμ κ°μ²΄μ κΈ°λ₯μ κ·Έλλ‘ λ¬Όλ €λ°κ² νλ κ²μ΄λ€.
μλλ Dogμ΄ Animalμ κΈ°λ₯μ μμνλ μμμ΄λ€.
μμ
function Animal(name) {
this.name = name;
}
Animal.prototype.move = function () {
console.log(this.name + " μ΄(κ°) μμ§μ
λλ€.");
};
function Dog(name) {
Animal.call(this, name); // Animal μμ±μ μ€ν
}
// νλ‘ν νμ
μμ μ€μ
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function () {
console.log(this.name + " λ©λ©!");
};
const d = new Dog("λ°λμ΄");
d.move(); // μμλ κΈ°λ₯
d.bark(); // Dog κ³ μ κΈ°λ₯
JavaScript
볡μ¬
μ€λͺ
β’
Object.create(Animal.prototype)
β Dog.prototype μ λΆλͺ¨λ₯Ό Animal.prototypeμΌλ‘ μ€μ
β’
Dog μΈμ€ν΄μ€λ Animalμ λ©μλλ₯Ό κ·Έλλ‘ μ¬μ©ν μ μλ€
β’
μ΄λ κ² κΈ°λ₯μ 곡μ νκ³ νμ₯νλ κ΅¬μ‘°κ° βνλ‘ν νμ
μμβ
ES6 ν΄λμ€μ νλ‘ν νμ μ κ΄κ³
ES6μμ class λ¬Έλ²μ΄ μΆκ°λμ§λ§, λ΄λΆ λμμ νλ‘ν νμ
μ μ΄μ©νλ€.
μλ class μ½λλ:
class Person {
constructor(name) {
this.name = name;
}
sayHello() {
console.log(`μλ
νμΈμ! ${this.name}`);
}
}
JavaScript
볡μ¬
μ¬μ€ λ΄λΆμ μΌλ‘ λ€μκ³Ό κ°μ κ΅¬μ‘°λ‘ λ³νλλ€:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function () {
console.log(`μλ
νμΈμ! ${this.name}`);
};
JavaScript
볡μ¬
μ¦, classλ λ¨μ§ νλ‘ν νμ
μ μ½κ² μ°λλ‘ νλ λ¬Έλ²μ μ€νμΌ λΏμ΄λ€.
μ 체 ν΅μ¬ μμ½
μ£Όμ | ν΅μ¬ μμ½ |
νλ‘ν νμ
μ μ | κ°μ²΄λ€μ΄ 곡μ νλ μν(λΆλͺ¨ κ°μ²΄) |
κ°μ²΄ μμ±κ³Ό νλ‘ν νμ
| new μ°μ° μ κ°μ²΄μ __proto__κ° μμ±μ.prototypeμ μ°κ²° |
νλ‘ν νμ
μ²΄μΈ | κ°μ²΄ β νλ‘ν νμ
β Object.prototype β null μμΌλ‘ μμ± νμ |
νλ‘ν νμ
μμ | ν μμ±μμ prototypeμ λ€λ₯Έ prototypeκ³Ό μ°κ²°ν΄ κΈ°λ₯μ μμ |
classμ νλ‘ν νμ
| classλ νλ‘ν νμ
κΈ°λ° μμμ λ¬Έλ²μ νΈμλ²μ |













