Javascript

extends, super

농담농담 2022. 9. 6. 16:41

class를 복사한다? 상속받는다? 확장한다? 개념.

이전에 작성한 class에서 추가할 때 사용하면 될듯.

class Dog{
    constructor(종류, 컬러){
        this.type = 종류,
        this.color = 컬러
    }
}

class Cat extends Dog{
    constructor(종류, 컬러, 나이){
        super(종류, 컬러)
        this.age = 나이
    }
}

var 강아지1 = new Dog('말티즈', 'white')//Dog { type: '말티즈', color: 'white' } 
var 강아지2 = new Dog('진돗개', 'gold')// Dog { type: '진돗개', color: 'gold' }
var 고양이1 = new Cat('샴', 'white', 20)//Cat { type: '샴', color: 'white', age: 20 }