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

 

  1. Human 클래슀의 μƒμ„±μž ν•¨μˆ˜λŠ” Humanμž…λ‹ˆλ‹€.
  2. Human 클래슀의 ν”„λ‘œν† νƒ€μž…μ€ Human 클래슀의 μΈμŠ€ν„΄μŠ€μΈ kimcoding의 __proto__μž…λ‹ˆλ‹€.
  3. Human 클래슀의 sleep λ©”μ„œλ“œλŠ” ν”„λ‘œν† νƒ€μž…μ— 있으며, Human 클래슀의 μΈμŠ€ν„΄μŠ€μΈ kimcodingμ—μ„œ kimcoding.sleep으둜 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

 

Humanμ΄λΌλŠ” ν΄λž˜μŠ€μ™€ μΈμŠ€ν„΄μŠ€, 그리고 ν”„λ‘œν† νƒ€μž…μ˜ 관계

 

 

Array(λ°°μ—΄) ν΄λž˜μŠ€μ™€ μΈμŠ€ν„΄μŠ€, 그리고 ν”„λ‘œν† νƒ€μž…μ˜ 관계

 

 

 

 

λ°˜μ‘ν˜•