๐Ÿคฟ (11) ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ - class

Kayยท2023๋…„ 5์›” 11์ผ
0

Javascript

๋ชฉ๋ก ๋ณด๊ธฐ
12/12

์ถœ์ฒ˜: mdn - classes
class๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ์ด๋‹ค.

๊ฐœ์š”

class๋Š” "ํŠน๋ณ„ํ•œ ํ•จ์ˆ˜"
ํ•จ์ˆ˜๋ฅผ ํ•จ์ˆ˜ํ‘œํ˜„์‹๊ณผ ํ•จ์ˆ˜ ์„ ์–ธ์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋“ฏ, class ํ‘œํ˜„์‹๊ณผ class ์„ ์–ธ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํด๋ž˜์Šค๋Š” ํ˜ธ์ด์ŠคํŒ… ๋  ๋•Œ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์„ ์–ธ ์ „ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

let avante = new Car('hyundai', 'avante', 'black'); // Reference Error

class Car {
	//
}

class ์„ ์–ธ

class ํ‚ค์›Œ๋“œ + ์ฃผ๋กœ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•  ๋‹จ์–ด

class Car {
  	// ์†์„ฑ ์ •์˜
	constructor(brand, name, color) {
    	this.brand = brand;
      	this.name = name;
      	this.color = color;
    }
  
  	// ๋ฉ”์„œ๋“œ ์ •์˜
  	drive() {
    	//
    }
}

classํ‘œํ˜„์‹

let Car = class {
	// unnamed class
}

console.log(Car.name);
// ์ถœ๋ ฅ: "Car"

let Car = class Carr {
	// named
}

console.log(Car.name);
// ์ถœ๋ ฅ: "Carr"

ํด๋ž˜์Šค ๋ณธ๋ฌธ๊ณผ ๋ฉ”์„œ๋“œ ์ •์˜

ํด๋ž˜์Šค ๋ณธ๋ฌธ(Class body)์€ ๋‹ค์Œ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ค‘๊ด„ํ˜ธ๋กœ ๋ฌถ์—ฌ ์žˆ๋Š” ์•ˆ์ชฝ ๋ถ€๋ถ„์ด๋‹ค.

class Car {
	//
}

๊ธฐ๋ณธ ๊ทœ์น™

ํด๋ž˜์Šค ๋ณธ๋ฌธ์€ Strict mode์—์„œ ์‹คํ–‰๋œ๋‹ค.

contructor

class๋กœ ์ƒ์„ฑ๋œ ๊ฐ์„ธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•œ ํŠน์ˆ˜ํ•œ ๋ฉ”์„œ๋“œ

  • ํด๋ž˜์Šค ์•ˆ์— ํ•œ ๊ฐœ๋งŒ ์กด์žฌํ•  ์ˆ˜ ์žˆ์Œ
  • ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get area() {
    return this.calcArea();
  }
  // ๋ฉ”์„œ๋“œ
  calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100

this ๋ฐ”์ธ๋”ฉ

ํด๋ž˜์Šค ๋ณธ๋ฌธ์€ Strict mode์—์„œ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— this ๋ฐ”์ธ๋”ฉ๋„ Strict mode๋ฅผ ๋”ฐ๋ฅธ๋‹ค. (์ „์—ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š์Œ)

class Animal {
  speak() {
    return this;
  }
  static eat() {
    return this;
  }
}

let obj = new Animal();
obj.speak(); // the Animal object

// call, apply, bind๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ
let speak = obj.speak;
speak(); // undefined

Animal.eat() // class Animal
let eat = Animal.eat;
eat(); // undefined

์ •์ (static) ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ

static ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—์„œ๋Š” ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค.
์ •์  ๋ฉ”์„œ๋“œ๋Š” ์ฃผ๋กœ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ •์  ์†์„ฑ์€ ๋ณต์ œํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๊ธฐํƒ€ ๋ฐ์ดํ„ฐ์— ์œ ์šฉํ•˜๋‹ค.

class Car {
	constructor(brand, name, color) {
    	this.brand = brand;
      	this.name = name;
      	this.color = color;
    }
  	
  	// ์ •์  ์†์„ฑ ์ •์˜
  	static privateValue = "privateValue";
  
  	// ์ •์  ๋ฉ”์„œ๋“œ ์ •์˜
  	static selfRepair() {
    	//
    }
}

let avante = new Car('hyundai', 'avante', 'black');

console.log(avante.privateValue) // undefined
console.log(avante.selfRepair()) // undefined
console.log(Car.privateValue); // privateValue

์ธ์Šคํ„ด์Šค ์†์„ฑ๊ณผ ์ •์  ์†์„ฑ ์ •์˜

์ธ์Šคํ„ด์Šค ์†์„ฑ์€ ๋ฐ˜๋“œ์‹œ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ ๋‚ด์— ์ •์˜๋˜์–ด์•ผ ํ•˜๋ฉฐ,
์ •์ (ํด๋ž˜์Šค ์‚ฌ์ด๋“œ) ์†์„ฑ๊ณผ ํ”„๋กœํ† ํƒ€์ž… ๋ฐ์ดํ„ฐ ์†์„ฑ์€ ๋ฐ˜๋“œ์‹œ ํด๋ž˜์Šค ์„ ์–ธ๋ถ€ ๋ฐ”๊นฅ์ชฝ์— ์ •์˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Public fields์™€ Instance fields

์ถœ์ฒ˜: velog - Class Public & Private field MDN ๋‚ด์šฉ ์ •๋ฆฌ
Public fields

  • Public static field
    • static ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ ๋ณ€์ˆ˜ ๋˜๋Š” ๋ฉ”์„œ๋“œ
      static name = 'name is only value'
    • ํด๋ž˜์Šค๋งˆ๋‹ค ๊ณ ์ •๋œ ๋‹จ ํ•œ๊ฐœ์˜ ํ•„๋“œ๊ฐ€ ์กด์žฌํ•˜๊ธฐ๋ฅผ ์›ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ
    • ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—์„œ๋Š” ์ ‘๊ทผ ๋ถˆ๊ฐ€
    • ์ž์‹ ํด๋ž˜์Šค์—์„œ๋Š” ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • Public instance field
    • ํด๋ž˜์Šค ๋ณธ๋ฌธ ๋‚ด ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š์€ ์ฑ„ ์ •์˜
    • ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ

Private fields

  • Private static field
    • private static ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ ๋ณ€์ˆ˜ ๋˜๋Š” ๋ฉ”์„œ๋“œ
      private static age = 0
    • ํด๋ž˜์Šค๊ฐ€ ์„ ์–ธ๋œ ๋‚ด๋ถ€์˜ ์ƒ์„ฑ์ž์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
    • ํด๋ž˜์Šค๊ฐ€ ์„ ์–ธ๋œ ๋‚ด๋ถ€์˜ ๋ฉ”์„œ๋“œ์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
    • ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€
    • ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€
  • Private instance field
    • ๋‚ด๋ถ€ ์ƒ์„ฑ์ž์—์„œ this๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
    • ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ์—์„œ this๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
    • ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋„๋ก ์บก์Šํ™”๊ฐ€ ๋จ

์€๋‹‰ํ™”(private ํ‚ค์›Œ๋“œ)์˜ ํ•œ๊ณ„

ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ private ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ง€์›ํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ ์–ด ๋งŽ์ด ์“ฐ์ด์ง€ ์•Š์Œ

extends & super๋ฅผ ํ†ตํ•œ ์ƒ์œ„ ํด๋ž˜์Šค ํ˜ธ์ถœ

ํด๋ž˜์Šค ์ƒ์†

class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

// extends ์ƒ์†
class Dog extends Animal {
  constructor(name) {
    // super class ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ name ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ
    super(name);
  }

  speak() {
    console.log(`${this.name} barks.`);
  }
}

let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.

ํด๋ž˜์Šค๋Š” ์žฌ์ •์˜๋  ์ˆ˜ ์—†๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€