๐ŸฆŽ [๋”ฅ๋‹ค์ด๋ธŒ ์Šคํ„ฐ๋””] 5th- 22. Class

1. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ Person์„ classํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•ด๋ณด์„ธ์š”.


let Person = (function () {
  function Person(name) {
    this.name = name;
  }

  Person.prototype.sayHi = function () {
    console.log("My name is " + this.name);
  };

  Person.sayHello = function () {
    console.log("Hello");
  };

  return Person;

//class์—์„œ๋Š” this ๋ฐ”์ธ๋”ฉ ๋๋‚˜๊ณ  ์ธ์Šคํ„ด์Šค (this) ๋ฐ˜ํ™˜ but f๋Š” ์ง์ ‘ return! 
//class ๊ฐ€ ์‹ ์‹! sugar syntax.. oop ๊ฐœ๋… ๋„์ž…์„ ์œ„ํ•ด..   

})();

//answer 
class Person {

//์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๋ฐ ์ดˆ๊ธฐํ™” 
constructor(name){
this.name = name; }

sayHi(){
console.log(`My name is ${this.name}`) 
}

static sayHello(){
console.log("Hello")}
}

//prototype์“ฐ๋Š” ์ด์œ ?  
// instance์ƒ์„ฑ๋•Œ๋งˆ๋‹ค ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜.. ๊ทธ๋ž˜์„œ ์”€. obj๋ฅผ ๋งค๋ฒˆ ์ฐ์–ด์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ
//์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์•„๋‹Œ, prototype์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ!์กฐ! => ์ด๊ฒฝ์šฐ prototype ์„ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ฐธ์กฐ. ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ. 


//์ฃผ์˜ํ• ์ 
//ํด๋ž˜์Šค ํ•„๋“œ์— ํ•จ์ˆ˜๋ฅผ ํ• ๋‹นํ•˜๋ฉด ์•ˆ๋จ. ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋จ. ํ”„๋กœํ†  ํƒ€์ž…์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ!! 
//์›๋ž˜๋Š” constructor ์•ˆ์˜ ๊ฐ’๋งŒ ์ถœ๋ ฅ ๋˜๊ณ  ๋ฉ”์„œ๋“œ๋Š” prototype ์•ˆ์— ์žˆ์Œ! 

2. ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„ ์ฐพ๊ณ  ์„ค๋ช…ํ•œ ๋’ค, ๊ณ ์ณ๋ณด์„ธ์š”.


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button class="btn">0</button>
  </body>
  <script>
    class App {
      constructor() {
        this.$button = document.querySelector(".btn");
        this.count = 0;
        this.$button.onclick = this.increase;
      }

      increase() {
        this.$button.textContent = ++this.count;
      }

/*answer
increase ๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ this ๊ฐ€ DOM (this.$button)์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. 
๋”ฐ๋ผ์„œ increase = () => this.$button.textContent = ++ this.content ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ 
์–ธ์ œ๋‚˜ ์ƒ์œ„ ์ปจํ…์ŠคํŠธ์˜ this ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•œ๋‹ค. (๋ฐ”์ธ๋”ฉ)
    
=> this. onclick ์— this.increase๊ฐ€ํ• ๋‹น๋˜๋ฉด.. increase ํ•จ์ˆ˜ ๋‚ด์˜ this๊ฐ€ this.button.button ์ด๋ ‡๊ฒŒ ๋จ!
ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ button์— ์ข…์†๋˜๋Š”๊ฒƒ. ์ด ๊ฒฝ์šฐ ์˜ˆ์ƒ๊ณผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ.(count ๊ฐ€ ์•ˆ์˜ฌ๋ผ๊ฐ)๊ฐ€ ๋ฐœ์ƒ๊ฐ€๋Šฅ.
    
ํ•ด๊ฒฐ์ฑ…
1. this.button.onclick = this.increase.bind(this)๋ฅผ constructor์—์„œ ํ•ด์„œ ๋ฐ”์ธ๋”ฉํ•˜๊ฑฐ๋‚˜ 
2. ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ  2. ()=>{} ์‚ฌ์šฉํ•ด์„œ ๋ฐ”์ธ๋”ฉ */

    
} 
    new App();
  </script>
</html>

3. Rectangle ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ color๋ฅผ ํ”„๋กœํผํ‹ฐ๋กœ ๊ฐ–๋Š” ColorRectangle ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”.

: ํ”„๋กœํ†  ํƒ€์ž… ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ์กด์˜ return๋ฌธ์„ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜์—ฌ, color๊ฐ€ ์ถ”๊ฐ€๋œ ๋ฌธ์žฅ์„ ๋ฐ˜ํ™˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ๋’ค ์ƒ์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์„ค๋ช…ํ•ด๋ณด์„ธ์š”.

class Rectangle {
  constructor(width, height) {
    this.width = width;
    this.height = height;
  }

  toString() {
    return `width = ${this.width}, height = ${this.height}`;
  }
}

//My Answer
class ColorRectangle extends Rectangle {
  constructor(width,height, color){
  //super๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ์— ์žˆ๋Š” ๊ฐ’์„ ์ตœ์ดˆ๋กœ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. 
  super(width,height);
  //color๋Š” ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ์ธ์ˆ˜์ด๋ฏ€๋กœ ์ถ”๊ฐ€
  this.color= color;
 }
 toString() {
   
  return super.toString()+`,color = ${this.color}`;
 }
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ