본문 바로가기
부트캠프교육중/JavaScript

클래스와 인스턴스2

by 뭉지야 2023. 1. 13.
728x90

클래스와 인스턴스를 다시 간략하게 정리해보겠다

 

Car

  • class
  • function Car(brand, name, color){}
  • function Car는 생성자함수이다(constructor)
  • 생성자함수는 class에서 인스턴스가 만들어질때 실행되는 코드이다.
  • 생성자함수는 return값을 만들지 않는다.
  • 클래스에 속성과 메소드를 정의하고 -> 인스턴스에서 이용한다
  • function car~ this brand ~ this name 이게 생성자함수이다.

Avante

  • instance
  • new키워드를 사용한다
  • new키워드는 새로운 인스턴스를 만드는방법이다. 변수에 클래스의 설계를 가진 인스턴스가 할당된다.
  • let avante = new Car('hyundai', 'avante', 'black')
  • 클래스에서 속성과 메소드를 정의하고 인스턴스에서 이용한다.

속성

ES5
function Car(brand, name, color) {    //여기가 생성자함수이다
this.brand = brand;
this.name = name;
this.color = color;
}

ES6
class Car {
constructor(brand, name, color) {
this.brand = brand;
this.name = name;
this.color = color;
}
}

 

메소드

ES5
function Car(brand, name, color) {생략}
Car.prototype.refuel = function(){}
Car.prototype.drive = function() {}

ES6
class Car {
constructor(brand, name, color) {생략}
refuel(){}
drive(){}
}

 

728x90

'부트캠프교육중 > JavaScript' 카테고리의 다른 글

내장고차함수(filter, map, reduce)  (0) 2023.01.14
일급객체와 고차함수  (0) 2023.01.14
프로토타입 (Prototype)  (0) 2023.01.13
객체 지향 프로그래밍  (0) 2023.01.13
클래스와 인스턴스 1  (0) 2023.01.13