클래스와 인스턴스

김혁중·2022년 3월 1일
0

JavaScript

목록 보기
1/23

1️⃣ 객체 지향 프로그래밍

  • Object Oriented JavaScript, OOP
  • 하나의 모델이 되는 청사진(blueprint)를 만들고, 그 청사진을 바탕으로 한 객체(object)를 만드는 프로그래밍 패턴
  • 청사진 -> class
  • 객체 -> instance

2️⃣ class 작성

3️⃣ constructor

  • 생성자 함수
  • return 값 없음
class Car {
  constructor(brand, name, color) {
    // 인스턴스가 만들어질 때 사용되는 코드
  }
}

3️⃣ instance

  • new 키워드 작성
let avante = new Car('hyundai', 'avante', 'black')

3️⃣ extends, super

4️⃣ extends

  • 클래스 상속

4️⃣ super

  • 부모 오브젝트의 함수를 호출할 때 사용
class avante extends Car {
  constructor(brand, name, color) {
    super()
    // 인스턴스가 만들어질 때 사용되는 코드
  }
}

class Sneakers {
  constructor(brand, name) {
    this.brand = brand
    this.name = name
  }
  show() {
    console.log(`이 신발은 ${this.brand}${this.name}입니다.`)
  }
}

class Adidas extends Sneakers {
  constructor(brand, name, size) {
    super(brand, name)
    this.size = size
  }
}

let superstar = new Adidas('adidas', 'superstar', 280)
console.log(superstar.brand) // adidas
console.log(superstar.name) // superstar
console.log(superstar.size) // 280
superstar.show() // 이 신발은 adidas의 superstar입니다.
profile
Digital Artist가 되고 싶은 초보 개발자

0개의 댓글