๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript

ํด๋ž˜์Šค

by ํ•ด-์˜จ 2023. 2. 24.

๐ŸŒผ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

class ClassName {
  constructor() {  }
  method1() {  }
  method2() {  }
  method3() {  }
}

 

ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ ๋’ค 'new ClassName()'์„ ํ˜ธ์ถœํ•˜๋ฉด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

constructor๋Š” new์— ์˜ํ•ด ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜๋ฉฐ, ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

class Fruit {
  constructor(name) {
    this.name = name;
  }
  
  say() {
    console.log(this.name);
  }
  
}

const apple = new Fruit('apple');
apple.say(); //apple

 

new Fruit๋ฅผ ํ•˜๋Š” ์ˆœ๊ฐ„ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  constructor๊ฐ€ ์ž๋™์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.

this.name์— 'apple'์ด ํ• ๋‹น๋˜๊ณ , ๋ฉ”์„œ๋“œ say๋ฅผ ์‹คํ–‰ํ•˜๋ฉด 'apple'์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

 

๐ŸŒผ getter์™€ setter

class์—์„œ๋Š” ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ์ธ get๊ณผ set์„ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ์ฝ๊ฑฐ๋‚˜ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‘˜ ๋‹ค ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ์•ž์— get, set ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

 

class Student {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  get fullName() {
    return `${this.lastName}${this.firstName}`;
  }

  set fullName(value) {
    console.log(value);
  }
}

const student = new Student('๊ธธ๋™', 'ํ™');
console.log(student.fullName);

 

get์€ ๊ฐ€์ ธ์˜ค๋Š” ์šฉ๋„์ด๋ฏ€๋กœ return์ด ํ•„์š”ํ•˜๊ณ 

set์€ ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๋Š” ์šฉ๋„์ด๋ฏ€๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

๐ŸŒผ ํด๋ž˜์Šค ํ•„๋“œ

ํด๋ž˜์Šค ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค ์ข…๋ฅ˜์˜ ํ”„๋กœํผํ‹ฐ๋„ ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

class Student {
  name = "๊ธธ๋™";

  greeting() {
    console.log(`${this.name} ์•ˆ๋…•!`);
  }
}

new Student().greeting(); // ๊ธธ๋™ ์•ˆ๋…•!

 

ํด๋ž˜์Šค ํ•„๋“œ๋Š” 'ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„ = ๊ฐ’'์„ ์“ฐ๋ฉด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

ํด๋ž˜์Šค ํ•„๋“œ๋Š” ๊ฐœ๋ณ„ ๊ฐ์ฒด์—๋งŒ ํด๋ž˜์Šค ํ•„๋“œ๊ฐ€ ์„ค์ •๋œ๋‹ค.

๋”ฐ๋ผ์„œ Student.prototype์—๋Š” ์„ค์ •๋˜์ง€ ์•Š๋Š”๋‹ค.

 

class Student {
  name = "๊ธธ๋™";
}

const student = new Student();
console.log(student.name); //๊ธธ๋™
console.log(Student.prototype.name); //undefined

 

๐ŸŒผ ์ ‘๊ทผ ์ œ์–ด์ž

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ public์ด๋‹ค.

pubilc ํ•„๋“œ๋Š” ์–ด๋””์„œ๋“  ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ ๋ˆ„๊ตฌ๋“  ์†์‰ฝ๊ฒŒ ์›ํ•˜๋Š” ๊ฐ’์— ์ ‘๊ทผํ•ด ๋ณ€๊ฒฝ์„ ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์œ„ํ—˜์ด ์žˆ๋‹ค.

 

๋”ฐ๋ผ์„œ private์œผ๋กœ ๋งŒ๋“ค์–ด ๋ฐ–์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“ ๋‹ค.

'#'์„ ๋ถ™์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ํด๋ž˜์Šค ์•ˆ์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

class Fruit {
  #name;

  constructor(name) {
    this.#name = name;
  }
}

const apple = new Fruit('apple');
apple.#name = 'orange';
console.log(apple); //SyntaxError: Private field '#name' must be declared in an enclosing class

 

ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ •์˜๋œ ํด๋ž˜์Šค์™€ ๊ทธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ protected๋„ ์žˆ๋‹ค.

protected ์—ญ์‹œ private์ฒ˜๋Ÿผ ๋‚ด๋ถ€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ˆจ๊ธธ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

'_'๋กœ ์‹œ์ž‘ํ•˜๋Š”๋ฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ง€์›ํ•˜๋Š” ๋ฌธ๋ฒ•์€ ์•„๋‹ˆ๊ณ  ๊ด€์Šต์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

๐ŸŒผ ์ •์  ๋ฉ”์„œ๋“œ์™€ ์ •์  ํ”„๋กœํผํ‹ฐ

prototype์ด ์•„๋‹Œ ํด๋ž˜์Šค ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ์ฐธ์กฐ/ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

class Student {
  static staticMethod() {
    console.log('static');
  }
}

Student.staticMethod(); // static

const student = new Student();
student.staticMethod(); //TypeError

 

์ •์  ํ”„๋กœํผํ‹ฐ์™€ ์ •์  ๋ฉ”์„œ๋“œ๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋กœ ์ฐธ์กฐ/ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค.

๋˜ํ•œ, ํด๋ž˜์Šค ๋ ˆ๋ฒจ์˜ ๋ฉ”์„œ๋“œ์—์„œ๋Š” this๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์—†๋‹ค.

 

class Product {
  constructor(name, expirationDate) {
    this.name = name;
    this.expirationDate = expirationDate;
  }

  static compareExpirationDate(product1, product2) {
    return product1.expirationDate - product2.expirationDate;
  }
}

let products = [
  new Product('apple', 2),
  new Product('banana', 3),
  new Product('orange', 1),
  new Product('lemon', 5),
];

products.sort(Product.compareExpirationDate);
console.log(products);
// [
//   Product { name: 'orange', expirationDate: 1 },
//   Product { name: 'apple', expirationDate: 2 },
//   Product { name: 'banana', expirationDate: 3 },
//   Product { name: 'lemon', expirationDate: 5 }
// ]

 

Product.compareExpirationDate๋Š” products๋ฅผ ๋น„๊ตํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ์œ„์—์„œ ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉ์‹์œผ๋กœ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ž˜์„œ ๊ฐ products๋“ค์˜ expiration date๋ฅผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

๐ŸŒผ ์ƒ์†

ํด๋ž˜์Šค๋Š” ์ƒ์†ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ์†์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ์˜ ํ™•์žฅ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

 

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

  eat() {
    console.log('๋จน๋Š”๋‹ค');
  }

  play() {
    console.log('๋…ผ๋‹ค');
  }
}

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

  eat() {
    console.log('๋จน๋Š”๋‹ค');
  }

  play() {
    console.log('๋…ผ๋‹ค');
  }

  drink(){
    console.log('์ˆ  ๋งˆ์‹ ๋‹ค');
    
  }
}

 

๋‘๊ฐœ์˜ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค.

ํ•˜๋‚˜๋Š” ๊ฐ•์•„์ง€, ํ•˜๋‚˜๋Š” ์‚ฌ๋žŒ์ด๋‹ค.

๋‘˜ ๋‹ค ์ด๋ฆ„์ด ์žˆ๊ณ , ๋จน๊ณ , ๋…ผ๋‹ค.

๋‹ค๋งŒ ๋‹ค๋ฅธ ๊ฒƒ์€ ์‚ฌ๋žŒ์€ ์ˆ ์„ ๋งˆ์‹ ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

class Dog์™€ Person์€ ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด ๋งŽ๋‹ค.

์ด๋•Œ, ์ƒ์†์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

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

  eat() {
    console.log('๋จน๋Š”๋‹ค');
  }

  play() {
    console.log('๋…ผ๋‹ค');
  }
}

class Dog extends Animal {}
const dog = new Dog('๋ฐ”๋‘‘์ด');
dog.play();

class Person extends Animal {}
const person = new Person('ํ™๊ธธ๋™');
person.eat();

 

Dog์™€ Person์€ Animal์ด๋ผ๋Š” ๊ณตํ†ต์ ์ด ์žˆ๊ธฐ์— class Animal์„ ๋งŒ๋“ค๊ณ  ๋‘˜์ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์„ ์ •๋ฆฌํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  extends๋ฅผ ํ†ตํ•ด ์ƒ์†๋ฐ›์œผ๋ฉด Animal์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ Dog์™€ Person๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

class Person extends Animal {
  constructor(name, hobby) {
    super(name);
    this.hobby = hobby;
  }

  call() {
    console.log(this.name, this.hobby);
  }
}
const person = new Person('ํ™๊ธธ๋™', '๊ณต๋†€์ด');
person.call(); //ํ™๊ธธ๋™, ๊ณต๋†€์ด

 

Animal์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” name์„ ์“ฐ๊ณ , ์ƒˆ๋กœ์šด hobby๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด?

super๋ฅผ ํ†ตํ•ด name์„ ์‚ฌ์šฉํ•˜๊ณ , hobby๋Š” ์ƒˆ๋กœ contructor์— ๋„ฃ์–ด์ค€๋‹ค.

 

class Person extends Animal {
  constructor(name, hobby) {
    super(name);
    this.hobby = hobby;
  }

  play() {
    super.play();
    console.log('๋†€๊ฑฐ์•ผ?');
  }
}
const person = new Person('ํ™๊ธธ๋™', '๊ณต๋†€์ด');
person.play();
//๋…ผ๋‹ค
//๋†€๊ฑฐ์•ผ?

 

๋ถ€๋ชจ ๋ฉ”์„œ๋“œ๋ฅผ ํ† ๋Œ€๋กœ ์ผ๋ถ€ ๊ธฐ๋Šฅ๋งŒ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋„ super๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์ด๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด๋ผ๊ณ  ํ•œ๋‹ค.

์œ„ ์ฝ”๋“œ์—์„œ play๋Š” ์ƒ์†๋ฐ›์€ Person์—๋„ ์กด์žฌํ•˜๋Š”๋ฐ super๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ์ž‘๋™ํ•œ๋‹ค๋ฉด Person.play์ธ '๋†€ ๊ฑฐ์•ผ?'๋งŒ ์ถœ๋ ฅ๋œ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ super๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Animal.play์ธ '๋…ผ๋‹ค'๋„ ํ•จ๊ป˜ ์ถœ๋ ฅ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

'JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

setTimeout๊ณผ setInterval  (0) 2023.11.02
๋ฐฐ์—ด  (0) 2023.02.26
๊ฐ์ฒด  (0) 2023.02.21
ํ•จ์ˆ˜  (0) 2023.02.19
์ œ์–ด๋ฌธ  (0) 2023.02.19

๋Œ“๊ธ€