클래스
클래스는 객체를 생성하기 위한 템플릿
인스턴스
비슷한 성질을 가진 여러개의 객체를 만들기 위해, 일종의 설계도(클래스)를 사용하여 생성한 객체를 인스턴스라고 함
ex)
하나의 모델이 되는 청사진 = 클래스
그 청사진을 바탕으로 한 객체 = 인스턴스
new 키워드
인스턴스를 만들 때에는 new 키워드를 사용
생성자 함수
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("보라");
alert(user.name); // 보라
alert(user.isAdmin); // false
ES5 클래스 작성 문법
클래스: 속성의 정의
function Car(brand, name, color) {
this.brand = brand;
this.name = name;
this.color = color;
}
클래스: 메소드의 정의
function Car(brand, name, 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;
}
}
클래스: 메소드의 정의
class Car {
constructor(brand, name, color) { /* 생략 */ }
refuel() {
}
drvie() {
}