객체지향 프로그래밍(OOP, Object-oriented-programming)은 데이터와 기능을 한 곳에서 묶어서 처리합니다.
속성과 메서드가 하나의객체
라는 개념에 포함되며class
라는 이름으로 부릅니다
객체지향 프로그래밍에서 클래스는 하나의 설계도(템플릿)를 뜻합니다
function Student (name, grade) {
this.name = name;
this.grade = grade;
}
Student.prototype.grade = function(){ // prototype : 원형이 되는 객체
return this.grade + '점'
}
class Student {
constructor(name, grade) { // constructor :인스턴스가 초기화 될때 실행
// this : 함수가 실행 될때마다 생성되는 고유한 실행컨텍스트
this.name = name;
this.grade = grade;
}
get grade() {
return `${this.grade} 점`
}
}
super
키워드를 통해 호출 할 수 있습니다stric mode
로 실행됩니다class Student {
constrouctor(name, grade) {
this.name = name;
this.grade = grade;
}
static category = "student"
}
필드를 먼저 선언함으로써 클래스의 정의는 self-ducumenting에 가까워졌고 필드는 언제나 존재하는 상태가 됩니다. 또한 필드 선언은 초기값(constructor)과 같이 설정 될 수도 있습니다
class Student {
grade = 0;
constrouctor(name, grade) {
this.name = name;
this.grade = grade;
}
}
class Student {
#grade = 0;
constrouctor(name, grade) {
this.name = name;
this.#grade = grade;
}
}
#
prefix를 추가하면 private 필드를 선언할 수 있습니다인스턴스는 클래스라는 설계도를 바탕으로 만들어진 객체를 인스턴스 라고 합니다
new
키워드를 통해 새로운 인스턴스를 생성합니다
실체
, 인스턴스는 관계
에 초첨을 맞춥니다