본문 바로가기
Codestates FE

[Codestates FE] S2U2 객체지향

by 진아링 2023. 3. 15.
728x90
반응형

클래스

ES5

// 클래스의 정의
function Car(brand, name, color) {
  // 속성의 정의
  this.brand = brand;
  this.name = name;
  this.color = color;
}

// 메소드의 정의
Car.prototype.refuel = function () {
}

// 인스턴스 생성
let avante = new Car('hyndai', 'avante', 'black');
// new라는 키워드를 통해 생성자 함수가 실행되고, 변수에 클래스의 설계를 가진 새로운 객체, 즉 인스턴스가 할당된다. 각각의 인스턴스는 클래스의 고유한 속성과 메서드를 갖게 된다.

// 인스턴스에서의 사용
avante.color;
avante.refuel();

ES6

// 클래스의 정의
class Car {
  // 인스턴스가 초기화될 때 실행하는 생성자 함수
  constructor(brand, name, color) {
    // 속성의 정의
    this.brand = brand;
    this.name = name;
    this.color = color;
  }

  // 메소드 정의
  refuel() {

  }
}
728x90
반응형

댓글