객체지향 프로그래밍
하나의 모델이 되는 예제(객체)를 만들고 그 예제를 가지고 여러개의 객체를 만드는 프로그래밍 패턴
클래스
하나의 모델 즉, 예제(객체)를 클래스로 지정
인스턴스
클래스를 바탕으로 한 객체
ES5와 ES6 문법 비교
ES5
function Car (brand, name, color){
this.brand = brand;
this.name = name;
this.color = color;
}
Car.prototype.drive = function() {
console.log(this.name + '가 운전을 시작합니다');
}
let avante = new Car('hyundai','avante','black');
avante.color; // 'black'
avante.drive(); //'avante가 운전을 시작합니다'
ES6
class Car { // ES5와 다르게 class 사용
constructor(brand, name, color) { // ES5와 다르게 constructor 사용
this.brnad = brand;
this.name = name;
this.color = color;
}
drive(){
return this.name + '가 운전을 시작합니다.'
}
}
let avante = new Car('hyundai','avate','black');
avante.color; // black
avante.drive(); //'avante가 운전을 시작합니다.
생성자 함수
ES5에 없었던 class와 Constructor 매서드를 볼 수 있다.
Constructor 매서드는 class의 인스턴스 객체를 생성하고 초기화하는 작동원리를 가지고 있다.
즉, 클로저 모듈 패턴과 매우 흡사한 역할을 한다.
new (operator)
new 연산자는 사용자 정의 객체 타입 또는 내장 객체 타입의 인스턴스를 생성한다.
즉,새로운 객체 타입의 인스턴스를 생성.
new constructor[([arguments])]
//constructor
//객체 인스턴스의 타입을 기술(명세)하는 함수
//arguments
//constructor와 함께 호출될 값 목록
'Learn > Javascript' 카테고리의 다른 글
프로토타입 (0) | 2023.01.13 |
---|---|
객체 지향 프로그래밍 (0) | 2023.01.13 |
DOM 정리 (0) | 2023.01.05 |
과제를 통해 새롭게 배운 것들 (0) | 2023.01.04 |
클로저 정리 (0) | 2023.01.03 |