codeStates front-end/Java Script
[JS] chapter17. νλ‘ν νμ κ³Ό ν΄λμ€
νν
ν¬
2023. 1. 13. 11:38
λ°μν
πνλ‘ν νμ κ³Ό ν΄λμ€
νλ‘ν νμ κΈ°λ° μΈμ΄
νλ‘ν νμ μ μν κ°μ²΄λ₯Ό μλ―Έ
κ°μ²΄μ νΉμ±μ λ€λ₯Έ κ°μ²΄λ‘ μμνλ κ²μ κ°λ₯νκ² νλ 맀컀λμ¦;]
__proto__
νΉμ κ°μ²΄μ νλ‘ν νμ κ°μ²΄μ μ κ·Όν μ μλ property
es6λΆν° getPrototypeof νΈμΆ
__proto__ μ .prototype μ μ°¨μ΄
__proto__
λͺ¨λ κ°μ²΄κ° κ°μ§κ³ μλ€.
νλμ Link λΌκ³ ν μ μλ€.
prototype
ν¨μ κ°μ²΄λ§ κ°μ§κ³ μλ€.
μμ±μλ₯Ό κ°μ§λ μνμΌλ‘ μ μΈ ν μ μλ€.
μ€μ΅μ½λ
class Human {
constructor(name, age) {
this.name = name;
this.age = age;
}
sleep() {
console.log(`${this.name}μ μ μ λ€μμ΅λλ€`);
}
}
let kimcoding = new Human('κΉμ½λ©', 30);
// μ€μ΅ν΄λ³΄μΈμ
Human.prototype.constructor === Human;
Human.prototype === kimcoding.__proto__;
Human.prototype.sleep === kimcoding.sleep;
answer : true, true, true
- Human ν΄λμ€μ μμ±μ ν¨μλ Humanμ λλ€.
- Human ν΄λμ€μ νλ‘ν νμ μ Human ν΄λμ€μ μΈμ€ν΄μ€μΈ kimcodingμ __proto__μ λλ€.
- Human ν΄λμ€μ sleep λ©μλλ νλ‘ν νμ μ μμΌλ©°, Human ν΄λμ€μ μΈμ€ν΄μ€μΈ kimcodingμμ kimcoding.sleepμΌλ‘ μ¬μ©ν μ μμ΅λλ€.
Humanμ΄λΌλ ν΄λμ€μ μΈμ€ν΄μ€, κ·Έλ¦¬κ³ νλ‘ν νμ μ κ΄κ³
Array(λ°°μ΄) ν΄λμ€μ μΈμ€ν΄μ€, κ·Έλ¦¬κ³ νλ‘ν νμ μ κ΄κ³
λ°μν