πΈ19μ₯. νλ‘ν νμ
π μλ°μ€ν¬λ¦½νΈλ λͺ
λ Ήν, ν¨μν, νλ‘ν νμ
κΈ°λ° κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ μ§μνλ λ©ν° ν¨λ¬λ€μ νλ‘κ·Έλλ° μΈμ΄μ΄λ€.
μμ νμ (primitive)
μ κ°μ μ μΈν λλ¨Έμ§ κ°λ€(ν¨μ, λ°°μ΄, μ κ·ννμ λ±
)μ λͺ¨λ κ°μ²΄λ€.μλ°μ€ν¬λ¦½νΈλ§μ
νλ‘ν νμ
κΈ°λ°μ κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ΄ μ‘΄μ¬νλ€.
β
19.1 κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°
κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ΄λ, μ¬λ¬ κ°μ λ
립μ λ¨μ, μ¦ **κ°μ²΄μ μ§ν©**μΌλ‘ νλ‘κ·Έλ¨μ νννλ €λ νλ‘κ·Έλλ° ν¨λ¬λ€μ
μΆμν(abstraction)
κ°μ²΄μ λ€μν μμ± μ€μμ νλ‘κ·Έλ¨μ νμν μμ±λ§ κ°μΆλ € νννλ κ²
κ°μ²΄
μ μ1. μμ±μ ν΅ν΄ μ¬λ¬ κ°μ κ°μ νλμ λ¨μλ‘ κ΅¬μ±ν 볡ν©μ μΈ μλ£κ΅¬μ‘°
μ μ2. μν λ°μ΄ν°μ λμμ νλμ λ Όλ¦¬μ μΈ λ¨μλ‘ λ¬Άμ 볡ν©μ μΈ μλ£κ΅¬μ‘°
κ°μ²΄λ 2κ°μ§ μμλ‘ κ΅¬μ±λλ€.
μν(state)
λ₯Ό λνλ΄λνλ‘νΌν°(property)
μνλ₯Ό μ‘°μνλ
λμ(behavior)
μ νννλλ©μλ(method)
β
19.2 μμκ³Ό νλ‘ν νμ
μμ(inheritance) : μ΄λ€ κ°μ²΄μ νλ‘νΌν°λ λ©μλλ₯Ό λ€λ₯Έ κ°μ²΄κ° μμλ°μ κ·Έλλ‘ μ¬μ©ν μ μλ κ²
μμ±μ ν¨μμμ μ΄ν΄λ΄€λ―μ΄, μμ±μ ν¨μλ λμΌν νλ‘νΌν° ꡬ쑰λ₯Ό κ°λ κ°μ²΄λ₯Ό μ¬λ¬ κ° μμ±ν λ μ μ©νλ€.
νμ§λ§, λ€μκ³Ό κ°μ λ¬Έμ κ° μλ€.
βCircleβκ°μ²΄ : λ°μ§λ¦, μμ λμ΄λ₯Ό ꡬνλ λ©μλλ‘ κ΅¬μ±λμ΄μλ€κ³ νμ.
κ°μ²΄μ λν΄ κ°κ°μ μμ κ°κΈ° λ€λ₯Έ λ°μ§λ¦μ κ°μ§ μ μμ§λ§, μμ λμ΄λ₯Ό ꡬνλ λ©μλλ λ°μ§λ¦ μ λ³΄λ§ μμΌλ©΄ κ³μ°μ λμΌνλ€.
μμ±μ ν¨μλ‘ κ°μ²΄λ₯Ό μ¬λ¬κ° μμ±νλ©΄, μ΄λ¬ν λ©μλλ κ³μν΄μ μμ±ν΄μ λ¬Έμ κ° μκΈ΄λ€.
β μμ΄λΌλ κ°μ²΄μ λν΄ λ°μ§λ¦μ λ 립μ μΌλ‘, μμ λ°μ§λ¦μ ν΅ν΄ ꡬν μ μλ μ 보(μμ λμ΄)λ 곡μ νλ κ²μ΄ ν¨μ¨μ μ΄λ€.
μ΄λ₯Ό κ°λ₯νλλ‘ νλ κ²μ΄ **μμ
**μ΄λ€.
μλ°μ€ν¬λ¦½νΈλ νλ‘ν νμ (prototype)μ κΈ°λ°μΌλ‘ μμμ ꡬννλ€.
// μμ±μ ν¨μ
function Circle(radius) {
// κ°κ°μ μ λ°μ§λ¦ λ°μ΄ν°(μν)λ λ
립μ
this.radius = radius;
}
// μμ λμ΄λ₯Ό ꡬνλ λ©μλλ Circle κ°μ²΄μ νλ‘ν νμ
μ λ±λ‘νμ¬ κ³΅μ
// νλ‘ν νμ
μ Circle μμ±μ ν¨μμ prototype νλ‘ν νμ
μ λ°μΈλ©λμ΄ μλ€.
Circle.prototype.getArea = function () {
return Math.PI * this.radius ** 2;
};
// μΈμ€ν΄μ€ μμ±
const circle1 = new Circle(1);
const circle2 = new Circle(2);
// Circle μμ±μ ν¨μκ° μμ±ν λͺ¨λ μΈμ€ν΄μ€λ
// λΆλͺ¨ κ°μ²΄ μν μ νλ νλ‘ν νμ
Circle.prototypeμΌλ‘λΆν° λ©μλλ₯Ό μμλ°λλ€.
console.log(circle1.getArea === circle2.getArea); // true (Circle μμ±μ ν¨μκ° μμ±νλ λͺ¨λ μΈμ€ν΄μ€λ νλμ getArea λ©μλλ₯Ό 곡μ νλ€. )
console.log(circle1.getArea()); // 3.141592653589793
console.log(circle2.getArea()); // 12.566370614359172
μμ±μ ν¨μκ° μμ±ν λͺ¨λ μΈμ€ν΄μ€λ μμ μ νλ‘ν νμ (β μμ(λΆλͺ¨) κ°μ²΄ μν μ νλ Circle.prototype)μ λͺ¨λ νλ‘ν νμ κ³Ό λ©μλλ₯Ό μμλ°λλ€.
μμ±μ ν¨μκ° μμ±ν λͺ¨λ μΈμ€ν΄μ€λ λΆλͺ¨ κ°μ²΄μΈ νλ‘ν νμ μ μμ°μ 곡μ νμ¬ μ¬μ©ν μ μλ€. λ°λΌμ, μμ±μ ν¨μκ° μμ±ν λͺ¨λ μΈμ€ν΄μ€κ° 곡ν΅μ μΌλ‘ μ¬μ©ν νλ‘νΌν°λ λ©μλλ₯Ό νλ‘ν νμ μ 미리 ꡬνν΄λλ©΄
μ½λ μ¬μ¬μ©
μΈ‘λ©΄μμ μ μ©νλ€.
β
19.3 νλ‘ν νμ
(κ°μ²΄)
λͺ¨λ κ°μ²΄λ νλμ νλ‘ν νμ
μ κ°λλ€.
κ·Έλ¦¬κ³ λͺ¨λ νλ‘ν νμ
μ μμ±μ ν¨μμ μ°κ²°λμ΄μλ€.
λ°λΌμ
κ°μ²΄ - νλ‘ν νμ - μμ±μν¨μ
λ μ°κ²°λμ΄ μλ€.νλ‘ν νμ μ μμ μ
constructor νλ‘νΌν°
λ₯Ό ν΅ν΄ μμ±μ ν¨μμ μ κ·Όν μ μκ³ , μμ±μν¨μλ μμ μprototype νλ‘νΌν°
λ₯Ό ν΅ν΄ νλ‘ν νμ μ μ κ·Όν μ μλ€.
λͺ¨λ κ°μ²΄λ [[Prototype]]μ΄λΌλ λ΄λΆ μ¬λ‘―μ κ°μ§λ€.
π [[Prototype]] λ΄λΆ μ¬λ‘―
μμ μ νλ‘ν νμ = κ°μ²΄κ° μμ μ [[Prototype]]μ ν΅ν΄ κ°λ¦¬ν€λ νλ‘ν νμ
[[Prototype]] λ΄λΆ μ¬λ‘―μ κ°μ
νλ‘ν νμ μ μ°Έμ‘°
λ€.[[Prototype]]μ μ μ₯λλ νλ‘ν νμ μ
κ°μ²΄ μμ± λ°©μ
μ μν΄ κ²°μ λλ€.(nullμΌ μ μμ)
ex) κ°μ²΄ 리ν°λ΄(
{}
)λ‘ μμ±λ κ°μ²΄ νλ‘ν νμ βObject.prototype
ex) μμ±μ ν¨μμ μν΄ μμ±λ κ°μ²΄ νλ‘ν νμ β
μμ±μ ν¨μμ prototype νλ‘νΌν°μ λ°μΈλ©λμ΄ μλ κ°μ²΄
[[Prototype]] λ΄λΆ μ¬λ‘―μλ μ§μ μ κ·Όν μ μκ³
__prototype__
μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ μμ μ νλ‘ν νμ μ κ°μ μ μΌλ‘ μ κ·Όν μ μλ€.
__proto__
μ κ·Όμ νλ‘νΌν°
__proto__
μ κ·Όμ νλ‘νΌν°λͺ¨λ κ°μ²΄λ __prototype__ μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ μμ μ νλ‘ν νμ
μ κ°μ μ μΌλ‘ μ κ·Όν μ μλ€.
λ΄λΆ μ¬λ‘―μ νλ‘νΌν°κ° μλκΈ° λλ¬Έμ, [[Prototype]] λ΄λΆ μ¬λ‘―μλ μ§μ μ κ·Όν μ μκ³
__proto__
μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ κ°μ μ μΌλ‘ μ κ·Όν μ μλ€.
__proto__
μ κ·Όμ νλ‘νΌν°λ μμμ ν΅ν΄ μ¬μ©λλ€.
__proto__
μ κ·Όμ νλ‘νΌν°λ Object.prototypeμ νλ‘νΌν°λ€.λͺ¨λ κ°μ²΄λ μμμ ν΅ν΄ Object.prototypeproto μ κ·Όμ νλ‘νΌν°λ₯Ό μ¬μ©ν μ μλ€.
__proto__
μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ νλ‘ν νμ
μ μ κ·Όνλ μ΄μ
μνΈ μ°Έμ‘°μ μν΄ νλ‘ν νμ 체μΈμ΄ μμ±λλ κ²μ λ°©μ§νκΈ° μν΄μλ€.
μλ‘κ° μμ μ νλ‘ν νμ μ΄ λλ λΉμ μμ μΈ νλ‘ν νμ μ²΄μΈ μμ
const parent = {}; const child = {}; child.__proto__ = parent; parent.__proto__ = child; // TypeError: Cyclic __proto__ value
νλ‘ν νμ 체μΈμ
λ¨λ°©ν₯ λ§ν¬λ 리μ€νΈ
λ‘ κ΅¬νλμ΄μΌ νλ€.νλ‘νΌν° κ²μ λ°©ν₯μ΄ νμͺ½ λ°©ν₯μΌλ‘λ§ νλ¬κ°μΌ νλ€.
__proto__
μ κ·Όμ νλ‘νΌν°λ₯Ό μ½λ λ΄μμ μ§μ μ¬μ©νλ κ²μ κΆμ₯νμ§ μλλ€.
λͺ¨λ κ°μ²΄κ°
__proto__
μ κ·Όμ νλ‘νΌν°λ₯Ό μ¬μ©ν μ μλ κ²μ μλκΈ° λλ¬Έμ΄λ€. β"μ§μ μμ"
λ°λΌμ,
__proto__
μ κ·Όμ νλ‘νΌν° λμνλ‘ν νμ μ μ°Έμ‘°λ₯Ό μ·¨λνκ³ μΆμ κ²½μ° β
Object.getPrototypeOf
λ©μλ μ¬μ©νλ‘ν νμ μ κ΅μ²΄νκ³ μΆμ κ²½μ° β
Object.setPrototypeOf
λ©μλ μ¬μ©
const obj = {}; const parent = { x: 1 }; // Object.getPropertyOf λ©μλλ‘ obj κ°μ²΄μ νλ‘ν νμ μ·¨λ console.log(Object.getPrototypeOf(obj)); // [Object: null prototype] {} // Object.setPrototypeOf λ©μλλ‘€ obj κ°μ²΄μ νλ‘ν νμ μ κ΅μ²΄ Object.setPrototypeOf(obj, parent); console.log(obj.__proto__); // { x: 1 }
ν¨μ κ°μ²΄μ prototype νλ‘νΌν°
ν¨μ κ°μ²΄λ§μ΄ μμ νλ prototype νλ‘νΌν°λ μμ±μ ν¨μκ° μμ±ν μΈμ€ν΄μ€μ νλ‘ν νμ μ κ°λ¦¬ν¨λ€.
μμ±μν¨μλ‘μ νΈμΆν μ μλ νμ΄νν¨μμ ES6 λ©μλ μΆμ½ ννμΌλ‘ μ μν λ©μλλ prototype νλ‘νΌν°λ₯Ό μμ νμ§ μκ³ νλ‘ν νμ λ μμ±νμ§ μλλ€.
// ν¨μ κ°μ²΄λ protoype νλ‘νΌν°λ₯Ό κ°μ§λ€. function () {}.hasOwnProperty("prototype"); // true // μΌλ° κ°μ²΄λ prototype νλ‘νΌν°λ₯Ό κ°μ§μ§ μλλ€. ({}.hasOwnProperty("prototype"); // false
λͺ¨λ κ°μ²΄κ° κ°μ§κ³ μλ __prototype__
μ κ·Όμ νλ‘νΌν°μ ν¨μ κ°μ²΄λ§μ΄ κ°μ§κ³ μλ prototype νλ‘νΌν°
λ λμΌν νλ‘ν νμ
μ κ°λ¦¬ν¨λ€.
function Person(name) {
this.name = name;
}
const me = new Person("Kwak");
console.log(me.__proto__ === Person.prototype); // true
λ€λ§ μ¬μ©νλ μ£Όμ²΄κ° λ€λ₯΄λ€.
ꡬλΆ
μμ
κ°
μ¬μ© 주체
μ¬μ© λͺ©μ
proto μ κ·Όμ νλ‘νΌν°
λͺ¨λ κ°μ²΄
νλ‘ν νμ μ μ°Έμ‘°κ°
λͺ¨λ κ°μ²΄
κ°μ²΄κ° μμ μ νλ‘ν νμ μ μ κ·Ό λλ κ΅μ²΄νκΈ°μν΄ μ¬μ©
prototype νλ‘νΌν°
constructor
νλ‘ν νμ μ μ°Έμ‘°κ°
μμ±μ ν¨μ
μμ±μ ν¨μκ° μμ μ΄ μμ±ν κ°μ²΄(μΈμ€ν΄μ€)μ νλ‘ν νμ μ ν λΉνκΈ° μν΄ μ¬μ©
νλ‘ν νμ
μ constructor νλ‘νΌν°μ μμ±μ ν¨μ
λͺ¨λ νλ‘ν νμ μ constructor νλ‘νΌν°λ₯Ό κ°λλ€.
constructor νλ‘νΌν°λ prototype νλ‘νΌν°λ‘ μμ μ΄ μ°Έμ‘°νκ³ μλ μμ±μ ν¨μλ₯Ό κ°λ¦¬ν¨λ€.
μ΄ μ°κ²°μ μμ±μ ν¨μκ° μμ±λ λ(=ν¨μ κ°μ²΄κ° μμ±λ λ) μ΄λ€μ§λ€.
function Person(name) {
this.name = name;
}
const me = new Person("Kwak");
// me κ°μ²΄μ μμ±μ ν¨μλ Personμ΄λ€.
console.log(me.constructor === Person); // true
π μμ±μ ν¨μμ μν΄ μμ±λ μΈμ€ν΄μ€λ νλ‘ν νμ μ constructor νλ‘νΌν°μ μν΄ μμ±μ ν¨μμ μ°κ²°λλ€.
Last updated