[TIL] 객체 지향 JavaScript

김용진·2021년 10월 3일
0

TIL

목록 보기
2/2
post-thumbnail

Achievement Goals

  • 클래스와 인스턴스라는 용어를 이해할 수 있다.
    - new 키워드의 사용법을 이해할 수 있다.
    - class 키워드의 사용법을 이해할 수 있다.
    - 현실 세계의 모델을 바탕으로 클래스의 메소드와 속성을 디자인할 수 있다.

  • 객체 지향 프로그래밍 특징을 이해할 수 있다.
    - 캡슐화(단위로 묶는 것, 은닉)
    - 상속
    - 추상화(인터페이스 단순화 사용)
    - 다형성

  • JavaScript에서 객체 지향 프로그래밍을 구현할 수 있다.
    - Prototype이 무엇인지 이해할 수 있다

  • 객체 지향 프로그래밍의 상속(Inheritance)의 개념을 이해하고 코드로 작성할 수 있다.
    - 상속관계를 가진 현실 세계의 모델을 코드로 작성할 수 있다.
    - 클래스 상속의 원리를 이해할 수 있다.
    - Prototype chain을 이해하고 설명할 수 있다. (__proto__)

학습 내용

ES6 class이용해 설계도 제작

class Bug {
    constructor(A,B,C,D){
      //parameter로 넘어온 값을 인스턴스 생성 시 속성 지정
      this.age=A
      this.color=B
      this.food=C
      this.job=D
    }
    eat(){//메소드
        return `Mmmmmm ${this.food}`
    }
}

설계도를 바탕으로 인스턴스 제작

//용진벌에 속성을 정해 instance를 만들수있다.
let Bee = new Bug(5,'yellow','honey','벌 키우기')
Bee.age // 5
Bee.eat() // 'Mmmmmm honey'

프로토타입

Bug.prototype.constructor === Bug
Bug.prototype === Bee.__proto__
Bug.prototype.eat === Bee.eat

상속 Inheritance

class HoneyMakerBee extends Bee{//Bee부터 상속받는다.
  constructor(color,food){
    super(color,food)//Bee속성 그대로 상속
    //추가,변동 사항
    this.age=10
    this.job='find pollen'
    this.canFly=true //parameter없이 설정
    this.treasureChest=[]
    }
    forage(treasure){ //메소드 추가
      this.treasureChest.push(treasure)
    }
}
profile
개발 블로그

0개의 댓글