속성과 메소드가 하나의 객체라는 개념에 포함되며, class
라는 이름으로 부릅니다.
let count = {
value :0,
increase: function(){
this.value++; //this는 count를 가리킴
}
decrease: function(){
this.value--;
},
getValue : function(){
return this.value
}
}
count.increase()
count.increase()
count.decrease()
count.getValue() // 1
singleton
패턴은 단 하나의 객체만 생성할 수 있다.
따라서 재사용을 위해서는 클로저 모듈패턴을 이용
function mkCount() {
return{
value:0,
increase :function() {
this.value++
},
decrease :function(){
this.value--;
},
getValue :function() {
return this.value;
}
},
}
let count1 = mkCount()
count.increase()
count.getValue() // 1
let count2 = mkCount()
count.decrease()
count.decrease()
count.getValue() //-2
하나의 모델이 되는 청사진class
을 만들고, 그 청사진을 바탕으로 한 객체instance
를 만드는 프로그래밍 패턴.
*생성자함수는 return값을 만들지 않음.
[Es5]-----------
function Car(brand, name, color) {
//instance 생성 코드
}
[Es6]-----------
class Car{
constructor(brand, name, color){
//instance 생성 코드
this.brand = brand;
this.name = name;
this.color = color;
}
}
인스턴스를 만들때에는 new키워드를 사용함
-->즉시 생성자함수가 실행되며, 변수에 인스턴스가 할당됨.
[Es5]------------------
Car.prototype.refuel = function(){
//refuel구현 코드
}
[Es6]------------------
class Car {
counstructor(brand, name, color{
//생량
}
refuel(){
}
}
prototype
모델의 청사진을 만들때 쓰는 원형객체
constructor
인스턴스가 초기화될 때 실행하는 생성자 함수
this
함수가 실행될 때, 해당 스코프마다 생성되는 고유한
실행 context.
class
원형폼으로, 객체를 생성하기 위한 청사진
instance
클래스의 사례 (instance object)
Encapsulation(캡슐화)
-실제 객체의 모습이나 추상화를 위해 필요한 데이터와 기능을 모았다 --> 느슨한결합
-은닉화: 다른객체나 메소드에서 이 클래스의 메소드를 활용하지 않으면, 내부에 접근불가
구현은 숨기고, 동작은 노출시킴
Inheritance(상속)
부모클래스->자식클래스
(부모클래스의 속성과 메소드를 자식 클래스가 활용할 수 있다)
Abstraction(추상화)
메소드 -->내부에 어떤 로직이 있는지 자세히 몰라도
이름만 보고 판단할 수 있도록
구현은 복잡하지만, 노출되는 부분은 단순하게 만듦.
*인터페이스: 클래스 정의시 메소드와 속성만 정의한것.
Polymorphism(다형성)
같은 메소드여도, 다른 방식으로 구현될 수 있음.
캡슐화: 코드 간결화, 재사용성 높임
추상화: 코드 간결화, 변화에 대한 영향 최소화
상속: 코드 간결화하여 재사용성 높임
다형성: 동일메소드를 객체특성에 따라 사용가능.