[SEB FE 44] 클래스와 인스턴스

Heechang Jeong·2023년 3월 15일
0

CODE STATES

목록 보기
20/40
post-thumbnail

✍ 복습 자료

  • 클로저 모듈 패턴

function makeAgeCounter() {
  let age = 20;
  return {
    increase: function() {
      age++;
    },
    decrease: function() {
      age--;
    },
    getAge: function() {
      return age;
    }
  }
}

let ageCounter1 = makeAgeCounter();
ageCounter1.decrease();
ageCounter1.getAge(); // 19

let ageCounter2 = makeAgeCounter();
ageCounter2.increase();
ageCounter2.increase();
ageCounter2.getAge() // 22

  • 클로저 모듈 패턴을 이용하는 이유?

    똑같은 기능을 하는 ageCounter를 여러 개 만들어서 같은 코드를 복사/붙여넣기 하지 않고 재사용할 수 있기 때문이다.


  • 클래스로부터 인스턴스를 만들 때 new 키워드를 사용

    즉시 생성자 함수가 실행되며, 변수에 클래스의 설계를 가진 새로운 객체인 인스턴스가 할당된다.
    각각의 인스턴스는 클래스의 고유한 속성과 메서드를 갖게 된다.

  • 속성과 메서드

    자동차의 속성은, 브랜드, 차 이름, 색상, 현재 연료 상태, 최고 속력 등이 있다.
    메서드는 "객체에 딸린 함수" => 연료 주입, 속력 설정, 운전 등이 있다.

  • ES6 class 작성 문법




📌 오늘의 알파

  • ES5 class 작성 문법과 ES6 class 작성 문법

// ES5
function Car(brand, name, color){
	this.brand=brand;
	this.name=name;
  	this.color=color;
}
Car.prototoype.drive = function{}

// ES6
class Car{
  	constructor(brand, name, color){
    	this.brand=brand;
      	this.name=name;
      	this.color=color;      
    }
  	drive(){}
}

0개의 댓글