하나의 모델이 되는 청사진(class)을 만들고, 그 청사진을 바탕으로 한 객체(instance)를 만드는 프로그래밍 패턴
function Car(Brand, Name, Color){
...
}//ES5
class Car{
constructor(Brand,Name,Color){
} //ES6, 최근에는 이 방법을 주로 사용한다.(constructor생성자함수)
}
let avante = new Car('hyundai', 'Avante', 'black');
//mdn 참고, ES5
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const car1 = new Car('Eagle', 'Talon TSi', 1993);
console.log(car1.make);
// Expected output: "Eagle"
클래스는 일종의 원형(original form), 객체를 생성하기 위한 아이디어이다. 클래스는 객체를 만들기 위한 생성자 함수를 포함한다. 인스턴스는 클래스의 사례(instance object)이다.
//mdn
function Person(first, last, age, gender, interests) {
// 속성과 메소드 정의
this.first = first;
this.last = last;
//...
}
var person1 = new Person('Bob', 'Smith', 32, 'male', ['music', 'skiing']);
캡슐화, 상속, 추상화, 다형성