[javascript] class 정리

class

자바스크립트는 기존의 클래스 중심의 객체지향 언어와는 달리 프로토타입 중심 의 언어이다.
따라서 class가 애초에 존재하지 않지만 class를 사용하는 여타 객체지향 언어(JAVA, Python, C++…) 에 익숙한 사람들의 수요에 의해 ES6부터 새롭게 적용 된 문법이다.
하지만 그럼에도 불구하고 자바스크립트는 프로토타입 중심 임에는 변함 없고, class 역시도 프로토타입을 기반으로 만들어진 것이다. (영상의 비유에 의하면 케잌 위의 설탕 모형 스러운 거라고…)

자바스크립트로 설정한 class에 typeof() 를 입혀보면 실은 function 임을 알 수 있다. JS에는 클래스 정말 없으니 속지 말자 😊 https://drive.google.com/uc?id=0B3Or0Wv2t1xwSUpZMTh3aDc5c0k

class를 이용한 예제를 보자.

class Mammal{
    constructor(sound){
        this._sound = sound;
        console.log("sound : ", this._sound);
    }

    talk(){
        console.log("talk : ", this._sound);
        return this._sound;
    }
}

console.log("type of mammal class : ",typeof(Mammal));

class Dog extends Mammal{
    constructor(){
        super("woof!!");
    }
}

let fluffy = new Dog();
fluffy.talk();

상속관계는 다음과 같이 확인할 수 있다.

https://drive.google.com/uc?id=0B3Or0Wv2t1xwdWtXRTZOWE5GOTg

참고 URL

Class keyword - Object Creation in JavaScript P7 - Fun Fun Function
Classes