[javascript] 클래스와 인스턴스

tnsdlznf23·2023년 3월 15일
0

class

  • 하나의 모델이 되는 청사진
  • 클래스명은 보통 대문자로 시작하며 일반명사로 만든다.

    #ES5 문법

    function Car(brand,name,color) {
    this.brand = brand;
    this.name = name;
    this.color = color; //속성정의
    }
    Car.prototype.refuel = function() {}
    Car.prototype.drive = function() {} //메소드 정의
    }

    #ES6 문법

    class Car {
    constructor(brand,name,color) { //생성자 함수
    this.brand = brand;
    this.name = name;
    this.color = color; //속성정의
    }
    refuel(){ }
    drive( ){ } //메소드 정의
    }

instance

  • class를 바탕으로 한 객체를 만드는 프로그래밍 패턴
  • new 키워드를 써서 함수를 정의한다.
  • 각각의 인스턴스는 클래스의 고유한 속성과 메서드를 갖게 된다.

    let avante(인스턴스) = new Car('hyundai','avante','black')
    avante.color; // 'black'

profile
프론트엔드 개발 기록장

0개의 댓글