Jieunny์˜ ๋ธ”๋กœ๊ทธ

S2) Unit 2. [JS] ํ”„๋กœํ† ํƒ€์ž… ๋ณธ๋ฌธ

CodeStates/learning contents

S2) Unit 2. [JS] ํ”„๋กœํ† ํƒ€์ž…

Jieunny 2023. 1. 13. 14:07

๐Ÿ“Œ JS๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜์˜ ์–ธ์–ด์ด๋‹ค.

๐Ÿ“ฃ ํ”„๋กœํ† ํƒ€์ž…

โœ”๏ธ ์›ํ˜•์ด๋ผ๋Š” ๋œป (๊ฐ์ฒด์˜ ์›ํ˜•, ์ฆ‰ ๊ฐ์ฒด์˜ ๋ถ€๋ชจ)
โœ”๏ธ ๊ฐ์ฒด๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ”„๋กœํ† ํƒ€์ž… ์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํ”„๋กœํ† ํƒ€์ž…์— ์ €์žฅ๋œ ์†์„ฑ๋“ค์ด ๊ทธ ๊ฐ์ฒด์— ์—ฐ๊ฒฐ๋œ๋‹ค.
โœ”๏ธ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ํ”„๋กœํ† ํƒ€์ž…์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“ฃ ํ”„๋กœํ† ํƒ€์ž…๊ณผ ํด๋ž˜์Šค

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; 		// true
Human.prototype === kimcoding.__proto__; 	// true
Human.prototype.sleep === kimcoding.sleep;	// true

โžฐ Human ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” Human์ด๋‹ค
โžฐ Human ํด๋ž˜์Šค์˜ ํ”„๋กœํ† ํƒ€์ž…์€ Human ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ kimcoding์˜ __proto__ ์ด๋‹ค.
โž• __proto__

๋”๋ณด๊ธฐ

์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์™€ ํ•จ๊ป˜ __proto__ ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ™์ด ์ƒ์„ฑ๋œ๋‹ค.

__proto__๋Š” kimcoding์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ Human ๊ฐ์ฒด์˜ prototype ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

โžฐ Human ํด๋ž˜์Šค์˜ sleep ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœํ† ํƒ€์ž…์— ์žˆ์œผ๋ฉฐ,
Human ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ kimcoding์—์„œ kimcoding.sleep์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ”๏ธ Human ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค, ํ”„๋กœํ† ํƒ€์ž…์˜ ๊ด€๊ณ„


โœ”๏ธ Array ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค, ํ”„๋กœํ† ํƒ€์ž…์˜ ๊ด€๊ณ„

์ƒ์†์˜ ๊ฒฝ์šฐ

โœ๏ธ .prototype์€ '์•„๋ž˜'๋กœ, .__proto__๋Š” '์œ„'๋กœ ๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž.

๐Ÿ“ฃ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ

โœ”๏ธ JS์—์„œ ์ƒ์†์„ ๊ตฌํ˜„ํ•  ๋•Œ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์„ ์‚ฌ์šฉํ•œ๋‹ค.

// Human ํด๋ž˜์Šค
let kimcoding = new Human('๊น€์ฝ”๋”ฉ', 30);

// ์†์„ฑ
kimcoding.age;
kimcoding.gender;
// ๋ฉ”์„œ๋“œ
kimcoding.eat();
kimcoding.sleep();


// Student ํด๋ž˜์Šค
let parkhacker = new Student('๋ฐ•ํ•ด์ปค', 22);

// ์†์„ฑ
parkhacker.grade;
// ๋ฉ”์„œ๋“œ
parkhacker.learn();

โžฐ Student ํด๋ž˜์Šค๋Š” Human ํด๋ž˜์Šค + ์ถ”๊ฐ€์ ์ธ ํŠน์ง•
โžฐ ๋ถ€๋ชจ ํด๋ž˜์Šค(Human) / ์ž์‹ ํด๋ž˜์Šค(Student) => ์ด๋ ‡๊ฒŒ ๋ฌผ๋ ค๋ฐ›๋Š” ๊ณผ์ •์ด ์ƒ์†
โžฐ JS์—์„œ๋Š” extends์™€ super ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์†์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

// Person ํด๋ž˜์Šค
class Person {
  constructor(first, last, age, gender, interests) {
    this.name = {
      first,
      last
    };
    this.age = age;
    this.gender = gender;
    this.interests = interests;
  }

  greeting() {
    console.log(`Hi! I'm ${this.name.first}`);
  };

  farewell() {
    console.log(`${this.name.first} has left the building. Bye for now!`);
  };
}


// Teacher ํด๋ž˜์Šค
class Teacher extends Person {
      constructor(first, last, age, gender, interests, subject, grade) {
          super(first, last, age, gender, interests);

          // subject and grade are specific to Teacher
          this.subject = subject;
          this.grade = grade;
  	}
}

โžฐ super() : ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ super()์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋ฅผ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
โžฐ Teacher์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ์˜๋„ํ•œ๋Œ€๋กœ Teacher์™€ Person ์–‘ ์ชฝ์˜ ๋ฉ”์†Œ๋“œ์™€ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ๏ธ ๋ฌธ์ œ

let div = document.createElement('div');

div.__proto__.__proto__	// HTMLElement
div.__proto__.__proto__.__proto__// Element

โœ๏ธ ๋ฌธ์ œ

class Human {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  sleep() {
    console.log(`${this.name}์€ ์ž ์— ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค`);
  }
}

let kimcoding = new Human('๊น€์ฝ”๋”ฉ', 30);

โžฐ Human์€ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋กœ์จ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ์—†๋‹ค. (โŒ)
-> ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋Š” Object๋กœ, Human์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๋Š” Object ์ด๋‹ค.

โœ๏ธ ๋ฌธ์ œ

let div = document.createElement('div');
document.body.append(div);
div.textContent = '์ฝ”๋“œ์Šคํ…Œ์ด์ธ ์—์„œ๋Š” ๋ชฐ์ž…์˜ ์ฆ๊ฑฐ์›€์„ ๋Š๋‚„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.';
div.addEventListener('click', () => console.log('๋ชฐ์ž…์„ ํ•˜๋ฉด ๋†€๋ผ์šด ์ผ์ด ์ƒ๊น๋‹ˆ๋‹ค.'));

โžฐ EventTarget์˜ prototype์— addEventListener ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค.
div๋Š” HTMLDivElement์˜ ์ธ์Šคํ„ด์Šค์ด๊ณ  EventTarget์„ ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— addEventListener๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ณดํ†ต ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” new ํ‚ค์›Œ๋“œ๋กœ ์ƒ์„ฑํ•˜์ง€๋งŒ, DOM์—์„œ๋Š” createElement๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

โœ๏ธ ๋ฌธ์ œ

class Human {
    constructor(name, age){
        this.name = name; 
        this.age = age;
    }
    sleep(){ return `${this.name}์ด(๊ฐ€) ์ž ์„ ์žก๋‹ˆ๋‹ค.`}
}

class Student extends Human{
    constructor(name, age, grade){
        super(name, age);
        this.grade = grade;
    }
    study(num){
        this.grade = this.grade + num;
        return `${this.name}์˜ ์„ฑ์ ์ด ${num}๋งŒํผ ์˜ฌ๋ผ ${this.grade}์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.`
    }
}

class ForeignStudent extends Student{
    constructor(name, age, grade, country){
        super(name, age, grade);
        this.country = country;
    }
    speak(){
        return `${this.country} ์–ธ์–ด๋กœ ์ˆ˜์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.`
    }
}

let americanStudent = new ForeignStudent('jake', 23, 80, '๋ฏธ๊ตญ');

โžฐ ForeignStudent์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๋Š” Student, Human ๋‘ ๊ฐœ์ด๋‹ค.