
JavaScript
// μ΄λ¦κ³Ό μ£Όμ μμ±μ κ°λ κ°μ²΄
const person = {
name: 'Hwang',
address: 'Busan'
};
console.log(person);
-> κ°μ²΄ : μμ±μ ν΅ν΄, μ¬λ¬ κ°μ κ°μ νλμ λ¨μλ‘ κ΅¬μ±ν 볡ν©μ μΈ μλ£κ΅¬μ‘°
const circle = {
radius: 5,
getDiameter() {
return 2 * this.radius;
},
getPerimeter() {
return 2 * Math.PI * this.radius;
},
getArea() {
return Math.PI * this.radius ** 2;
}
};
console.log(circle);
console.log(circle.getDiameter());
console.log(circle.getPerimeter());
console.log(circle.getArea());

λ°μ§λ¦ : μμ μνλ₯Ό λνλ΄λ λ°μ΄ν°
μ§λ¦, λλ , λμ΄ : λμ
κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ
λ°μ΄ν°μ λμμ νλμ λ Όλ¦¬μ λ¨μλ‘ λ¬Άμ΄ μκ°.
κ° κ°μ²΄λ λ 립μ μΌλ‘ μ‘΄μ¬ν μ μκ³ , λ€λ₯Έ κ°μ²΄μ κ΄κ³μ±μ κ°μ§ μλ μλ€.
// μμ±μ ν¨μ
function Circle(radius) {
this.radius = radius;
this.getArea = function() {
return Math.PI * this.radius ** 2;
};
}
// λ°μ§λ¦μ΄ 1,2μΈ μΈμ€ν΄μ€ μμ±
const circle1 = new Circle(1);
const circle2 = new Circle(2);
console.log(circle1.getArea === circle2.getArea);
console.log(circle1.getArea());
console.log(circle2.getArea());

λͺ¨λ μΈμ€ν΄μ€κ° λμΌν λ΄μ©μ methodλ₯Ό μ¬μ©ν κ²½μ°?
-> methodλ₯Ό λ¨ νλλ§ μμ±νμ¬, λͺ¨λ μΈμ€ν΄μ€κ° 곡μ ν΄μ μ¬μ©νλλ‘ νλ κ²μ΄ λ°λμ§ν¨
νμ§λ§ μμ μ½λμμ, getArea methodλ λͺ¨λ μΈμ€ν΄μ€κ° μ€λ³΅ μμ νκ² λ¨
-> λΆνμν λ©λͺ¨λ¦¬ λλΉ
function Circle(radius) {
this.radius = radius;
}
// νλ‘ν νμ
μ κΈ°λ°μΌλ‘ μμμ ꡬν!!!
Circle.prototype.getArea = function () {
return Math.PI * this.radius ** 2;
};
const circle1 = new Circle(1);
const circle2 = new Circle(2);
console.log(circle1.getArea === circle2.getArea);
console.log(circle1.getArea());
console.log(circle2.getArea());

: κ°μ²΄ κ° μμμ ꡬννκΈ° μν΄ μ¬μ©λ¨
prototype : μ΄λ€ κ°μ²΄μ μμ(λΆλͺ¨) κ°μ²΄μ μν μ νλ κ°μ²΄
-> λ€λ₯Έ κ°μ²΄μ 곡μ property(method ν¬ν¨)λ₯Ό μ 곡
prototypeμ μμλ°μ νμ(μμ) κ°μ²΄
-> μμ κ°μ²΄μ propertyλ₯Ό μμ μ propertyμ²λΌ μμ λ‘κ² μ¬μ©ν μ μλ€.
[[Prototype]] λΌλ λ΄λΆ μ¬λ‘―μ κ°μ§__proto__ μ κ·Όμ νλ‘νΌν°__proto__ μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ [[Prototype]] λ΄λΆ μ¬λ‘―μ κ°μ μ κ·Ό κ°λ₯[[Prototype]] : κ°μ²΄ μμ μ prototype
__proto__λ μ κ·Όμ νλ‘νΌν°λ€.
[[Value]] propertyλ₯Ό κ°μ§ μκ³ , [[Get]] [[Set]] property attributeλ‘ κ΅¬μ±λ property.[[Get]] [[Set]] : λ€λ₯Έ data propertyμ κ°μ μ½κ±°λ μ μ₯ν λ μ¬μ©νλ μ κ·Όμ ν¨μObject.property μ μ κ·Όμ νλ‘νΌν°μΈ __proto__
-> getter/setter ν¨μλΌκ³ λΆλ₯΄λ μ κ·Όμ ν¨μλ₯Ό ν΅ν΄
[[Property]] λ΄λΆ μ¬λ‘―μ κ°, μ¦ νλ‘ν νμ
μ μ·¨λνκ±°λ ν λΉνλ€.
__proto__ μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ νλ‘ν νμ
μ μ κ·Όνλ©΄,
λ΄λΆμ μΌλ‘ __proto__ μ κ·Όμ νλ‘νΌν°μ getter ν¨μμΈ [[Get]]μ΄ νΈμΆλλ€.
__proto__ μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ μλ‘μ΄ νλ‘ν νμ
μ ν λΉνλ©΄,
__proto__ μ κ·Όμ νλ‘νΌν°μ setter ν¨μμΈ [[Set]]μ΄ νΈμΆλλ€.
const obj = {};
const parent = { x: 1 };
// setter ν¨μμΈ set __proto__κ° νΈμΆλ¨
// -> obj κ°μ²΄μ νλ‘ν νμ
μ κ΅μ²΄
obj.__proto__ = parent;
console.log(obj.x); // 1
__proto__μ κ·Όμ νλ‘νΌν°λ μμμ ν΅ν΄ μ¬μ©λλ€.
__proto__ μ κ·Όμ νλ‘νΌν°λ κ°μ²΄κ° μ§μ μμ νλ νλ‘νΌν°κ° μλλ€.
-> Object.prototypeμ νλ‘νΌν°μ
λͺ¨λ κ°μ²΄λ μμμ ν΅ν΄ Object.prototype.__proto__ μ κ·Όμ νλ‘νΌν°λ₯Ό μ¬μ©ν μ μλ€.
const person = { name: 'Hwang' };
// person κ°μ²΄λ __proto__ νλ‘νΌν°λ₯Ό μμ νμ§ μλλ€
console.log(Object.hasOwnProperty('__proto__'));
// __proto__ νλ‘νΌν° : λͺ¨λ κ°μ²΄μ νλ‘ν νμ
κ°μ²΄μΈ Object.prototypeμ μ κ·Όμ νλ‘νΌν°
console.log(Object.getOwnPropertyDescriptor(Object.prototype, '__proto__'));
// λͺ¨λ κ°μ²΄λ Object.prototypeμ μ κ·Όμ νλ‘νΌν° __proto__λ₯Ό μμλ°μ μ¬μ©ν μ μλ€.
console.log({}.__proto__ === Object.prototype);

__proto__μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ νλ‘ν νμ μ μ κ·Όνλ μ΄μ ?
[[Property]] λ΄λΆ μ¬λ‘―μ κ°μ μ κ·ΌνκΈ° μν΄ μ κ·Όμ νλ‘νΌν°λ₯Ό μ¬μ©νλ μ΄μ ?
=> μνΈ μ°Έμ‘°μ μν΄ νλ‘ν νμ
체μΈμ΄ μμ±λλ κ²μ λ°©μ§νκΈ° μν¨
const parent = {};
const child = {};
// childμ νλ‘ν νμ
μ parentλ‘ μ€μ
child.__proto__ = parent;
// parentμ νλ‘ν νμ
μ childλ‘ μ€μ
parent.__proto__ = child;
// μ΄λ° μ½λκ° error μμ΄ μ μμ μΌλ‘ μ²λ¦¬λλ©΄?
// -> μλ‘κ° μμ μ νλ‘ν νμ
μ΄ λλ λΉμ μμ μΈ νλ‘ν νμ
체μΈμ΄ λ§λ€μ΄μ§
// -> error λ°μ ν¨
-> νλ‘ν νμ
체μΈμ λ¨λ°©ν₯ linked listλ‘ κ΅¬νλμ΄μΌ ν¨
(property κ²μ λ°©ν₯μ΄ νμͺ½ λ°©ν₯μΌλ‘λ§ νλ¬κ°μΌ ν¨)
μν μ°Έμ‘°νλ νλ‘ν νμ
체μΈμ΄ λ§λ€μ΄μ§λ©΄?
-> property κ²μμ, 무ν 루νμ λΉ μ§
<- νλ‘ν νμ
체μΈμ μ’
μ μ΄ μ‘΄μ¬νμ§ μκΈ° λλ¬Έ
κ·Έλμ?
μλ¬΄λ° μ²΄ν¬ μμ΄ λ¬΄μ‘°κ±΄μ μΌλ‘ νλ‘ν νμ
μ κ΅μ²΄ν μ μλλ‘ ν¨
__proto__ μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄μ νλ‘ν νμ
μ μ κ·Όνκ³ κ΅μ²΄νλλ‘ κ΅¬νλμ΄ μμ
__proto__μ κ·Όμ νλ‘νΌν°λ₯Ό μ½λ λ΄μμ μ§μ μ¬μ©νλ κ²μ κΆμ₯νμ§ μλλ€.
μ½λ λ΄μμ __proto__ μ κ·Όμ νλ‘νΌν°λ₯Ό μ§μ μ¬μ©νλ κ²μ κΆμ₯νμ§ μμ.
λͺ¨λ κ°μ²΄μ¬ __proto__ μ κ·Όμ νλ‘νΌν°λ₯Ό μ¬μ©ν μ μλ κ²μ΄ μλκΈ° λλ¬Έ
μ§μ μμμ ν΅ν΄ Object.prototypeμ μμλ°μ§ μλ κ°μ²΄λ₯Ό μμ±ν μλ μκΈ° λλ¬Έμ, __proto__ μ κ·Όμ νλ‘νΌν°λ₯Ό μ¬μ©ν μ μλ κ²½μ°κ° μμ μ μμ
// objλ prototype chainμ μ’
μ .
// -> Object.__proto__λ₯Ό μμλ°μ μ μλ€.
const obj = Object.create(null);
console.log(obj.__proto__);
// -> __proto__λ³΄λ€ Object.getPrototypeOd λ©μλλ₯Ό μ¬μ©νλ νΈμ΄ μ’λ€.
console.log(Object.getPrototypeOf(obj));
// -----------------------------------------------------------------------
const obj = {};
const parent = { x: 1 };
// obj κ°μ²΄μ prototypeμ μ·¨λ―
Object.getPrototypeOf(obj);
// obj κ°μ²΄μ prototypeμ κ΅μ²΄
Object.setPrototypeOf(obj, parent);
console.log(obj.x);

prototype νλ‘νΌν°λ, μμ±μ ν¨μκ° μμ±ν κ°μ²΄μ νλ‘ν νμ
μ κ°λ¦¬ν¨λ€.
λ°λΌμ μμ±μ ν¨μλ‘μ νΈμΆν μ μλ ν¨μ, μ¦ non-constructor μΈ νμ΄ν ν¨μμ ES6λ©μλ μΆμ½ ννμΌλ‘ μ μν λ©μλλ prototype νλ‘νΌν°λ₯Ό μμ νμ§ μμΌλ©°, νλ‘ν νμ
λ μμ±νμ§ μλλ€.
(μμ μ€)