클래스 (class)

PYG·2021년 5월 18일

OOP

목록 보기
5/8

클래스 (Class)

  • 메소드만으로 프로그램을 만드는 건 부족하다
    그래서 서로 연관된 메소드와 변수들을 모아서 그룹핑하고 거기에 이름을 붙여서 정리정돈을 하고 싶어진다
    그래서 만들어진 수납상자가 class

ex) 아이스크림, 유제품

1.1

function Person(name, first, second){
    this.name = name;
    this.first = first;
    this.second = second;
    this.sum = function(){
        return this.first + this.second;
    }
}

1.2 - 1.1을 class Person으로 변경 시

class Person {
    constructor(name, first, second){
        this.name = name;
        this.first = first;
        this.second = second;
    }
    sum() {
        return 'prototype : '+ (this.first + this.second);
    }
}
  • 꼭 class 안에 constructor를 써야 작동

0개의 댓글