<aside> ๐Ÿ’ก javascript์˜ ํด๋ž˜์Šค ๋ฌธ๋ฒ•์€ ES6์—์„œ ๋„์ž…๋˜์—ˆ๋‹ค.

</aside>

1. ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค์˜ ์ผ๋ฐ˜์ ์ธ ๊ฐœ๋…

2. Class๋ž€?

class Person {
	// constructor๋Š” ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์–ด์š”.
  constructor(name, age) {
		// ์ด๋ฆ„(name)๊ณผ ๋‚˜์ด(age)๊ฐ€ ์—†์œผ๋ฉด ์‚ฌ๋žŒ์ด ์•„๋‹ˆ์ฃ ?
		// new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ, ๊ธฐ๋ณธ์ ์œผ๋กœ
		// ๋„ฃ์–ด์•ผ ํ•˜๋Š” ๊ฐ’๋“ค์„ ์˜๋ฏธํ•ด์š”! :)
		// ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” this๋Š” ๋งŒ๋“ค์–ด์งˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์˜๋ฏธํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์ฃผ์„ธ์š”!
    this.name = name;
    this.age = age;
  }

	// ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ์–ด์š”.
	// ์—ฌ๊ธฐ์„œ this.name์œผ๋กœ ๋‚ด๋ถ€ ๊ฐ’์„ ์ ‘๊ทผํ•ด์•ผ ํ•จ์„ ์žŠ์ง€ ๋งˆ์„ธ์š”! :)
  sayHello() {
    console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
  }
}

const person1 = new Person("Alice", 30);
const person2 = new Person("Bob", 25);

// ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ํ† ๋Œ€๋กœ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœํ•ด๋ณด๊ธฐ
person1.sayHello(); // ์ถœ๋ ฅ: "Hello, my name is Alice and I am 30 years old."
person2.sayHello(); // ์ถœ๋ ฅ: "Hello, my name is Bob and I am 25 years old."

Person Class๋Š” name๊ณผ age ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, sayHello ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค. newํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Person Class์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , sayHello ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

3. Constructor๋ž€?

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

  sayHello() {
    console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
  }
}

const person = new Person('Alice', 20);

constructor ๋ฉ”์„œ๋“œ๋Š” name๊ณผ age๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ this.name๊ณผ this.age ์†์„ฑ์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

4. Getter์™€ Setter

// Getters์™€ Setters
// ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด -> G, S
// ํด๋ž˜์Šค --> ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)
// ํ”„๋กœํผํ‹ฐ(constructor)
// new Class(a, b, c)
class Rectangle {
  constructor(height, width) {
    // underscore : private(์€๋ฐ€ํ•˜๊ณ , ๊ฐ์ถฐ์•ผ ํ•  ๋•Œ)
    this._height = height;
    this._width = width;
  }

  // width๋ฅผ ์œ„ํ•œ getter
  get width() {
    return this._width;
  }

  // width๋ฅผ ์œ„ํ•œ setter
  set width(value) {
    // ๊ฒ€์ฆ 1 : value๊ฐ€ ์Œ์ˆ˜์ด๋ฉด ์˜ค๋ฅ˜!
    if (value <= 0) {
      //
      console.log("[์˜ค๋ฅ˜] ๊ฐ€๋กœ๊ธธ์ด๋Š” 0๋ณด๋‹ค ์ปค์•ผ ํ•ฉ๋‹ˆ๋‹ค!");
      return;
    } else if (typeof value !== "number") {
      console.log("[์˜ค๋ฅ˜] ๊ฐ€๋กœ๊ธธ์ด๋กœ ์ž…๋ ฅ๋œ ๊ฐ’์ด ์ˆซ์žํƒ€์ž…์ด ์•„๋‹™๋‹ˆ๋‹ค!");
      return;
    }
    this._width = value;
  }

  // height๋ฅผ ์œ„ํ•œ getter
  get height() {
    return this._height;
  }

  // height๋ฅผ ์œ„ํ•œ setter
  set height(value) {
    // ๊ฒ€์ฆ 1 : value๊ฐ€ ์Œ์ˆ˜์ด๋ฉด ์˜ค๋ฅ˜!
    if (value <= 0) {
      //
      console.log("[์˜ค๋ฅ˜] ์„ธ๋กœ๊ธธ์ด๋Š” 0๋ณด๋‹ค ์ปค์•ผ ํ•ฉ๋‹ˆ๋‹ค!");
      return;
    } else if (typeof value !== "number") {
      console.log("[์˜ค๋ฅ˜] ์„ธ๋กœ๊ธธ์ด๋กœ ์ž…๋ ฅ๋œ ๊ฐ’์ด ์ˆซ์žํƒ€์ž…์ด ์•„๋‹™๋‹ˆ๋‹ค!");
      return;
    }
    this._height = value;
  }

  // getArea : ๊ฐ€๋กœ * ์„ธ๋กœ => ๋„“์ด
  getArea() {
    const a = this._width * this._height;
    console.log(`๋„“์ด๋Š” => ${a}์ž…๋‹ˆ๋‹ค.`);
  }
}

// instance ์ƒ์„ฑ
const rect1 = new Rectangle(10, 7);
rect1.getArea();
// const rect2 = new Rectangle(10, 30);
// const rect3 = new Rectangle(15, 20);

5. ์ƒ์† (Inheritance)

<์˜ˆ์‹œ>

Dog Class๋Š” Animal Class๋ฅผ ์ƒ์†๋ฐ›๊ณ , Dog Class์—์„œ speak ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜์—ฌ Animal Class์˜ speak๋ฉ”์„œ๋“œ๋ฅผ ๋ฎ์–ด์“ด๋‹ค.

// ๋™๋ฌผ ์ „์ฒด์— ๋Œ€ํ•œ ํด๋ž˜์Šค์—์š”
class Animal {

	// ์ด๋ฆ„์„ ํ•„์ˆ˜๋กœ ๋ฐ›์•„์•ผ ํ•ด์š”
  constructor(name) {
    this.name = name;
  }

	// ๋™๋ฌผ์˜ ํ–‰๋™์„ ์ •์˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

// ๋™๋ฌผ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” Dog ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์š”
class Dog extends Animal {
	// ์ƒ์†๋ฐ›์„ ๋•Œ, speak()๋ฅผ ์ž…๋ง›์— ๋งž๊ฒŒ ์žฌ์ •์˜ํ•ด์š”.
  speak() {
    console.log(`${this.name} barks.`);
  }
}

// Dog๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” Animal์˜ ์ƒ์†์„ ๋ฐ›์€ class์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฆ„์„ ํ•„์ˆ˜๋กœ
// ๋ฐ›์•„์•ผ ํ•ด์š”!
let d = new Dog('Mitzie');

// speak๋Š” 'makes a noise'๊ฐ€ ์•„๋‹ˆ๋ผ, 'barks'๊ฐ€ ์ถœ๋ ฅ๋˜๋„ค์š”.
d.speak(); // "Mitzie barks."

6. Static Method

<aside> ๐Ÿ’ก ์ฆ‰, ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

</aside>

class Calculator {
  static add(a, b) {
    return a + b;
  }

  static subtract(a, b) {
    return a - b;
  }
}

console.log(Calculator.add(1, 2)); // 3
console.log(Calculator.subtract(3, 2)); // 1