
청사진을 만들고, 그걸 바탕으로한 객체를 만드는 프로그래밍 패턴.
자동차 만들 때 설계도 바탕으로 각 모델 만드는 거 생각하면 된다.
설계도는 class고 자동차 모델 종류는 instance object임
객체를 생성하기 위한 템플릿
ES6 에서 : 클래스 생성자를 이용class 키워드를 사용해야한다 → 클래스 안에는 생성자(constructor) 가 있음
템플릿을 바탕으로 만든 객체
new 키워드를 사용한다. (new + 클래스 명)avante , m2) 는 클래스의 고유한 속성&메소드 가짐let avante = new car('hyundai', 'avante', 'black');
let m2 = new car('bmw', 'm2', 'grey');
class Car {
// 속성의 정의
constructor(brand, name, color) {
// 인스턴스가 만들어질 때 실행되는 코드
// 여기서 this는 인스턴스 객체를 가리킨다
this.brand = brand;
this.name = name;
this.color = color;
} // this는 인스턴트 객체를 의미하고, 만들어진 인스턴트에 해당 브랜드, 이름,색상을 부여하겠다는 의미이다.
// 메서드 정의
refuel(){
console.log(`${this.name} 에 연료를 공급합니다.`);
}
drive() {
console.log(`${this.brand} 가 주행을 시작합니다.`);
}
}
여기서 constructor() 라는 메서드는 생성자 함수라고 불린다.
constructor() 메서드는 class 의 인스턴스 객체를 생성하고 초기화할 수 있다. 그래서 따로 return 문이 필요없다.
constructor() 메서드는 클래스 안에서 딱 한 개만 존재할 수 있다.
class 함수 안에서 간단하게 메소드이름(){} 의 형태로 정의할 수 있다.
ES6 (클래스 선언) : 클래스 생성자를 이용하는 방법class 키워드를 사용해야한다 → 클래스 안에는 생성자(constructor) 가 있음class Rectangle { //class 키워드 사용
constructor(height, width, color) {
this.height = height;
this.width = width;
this.color = color;
}
}
todo list
_
나만 못따라가고 있는거 같다..
수업자료로는 도저히 어려워서 생활코딩 듣는중
한달만 더 버텨보자ㅏ..😶🌫️_