constructor(color, width, height) {
super();
this.color = color;
this.width = width;
this.height = height;
}
super()를 인자 없이 호출.Shape)의 생성자를 아무 인자 없이 호출color 속성을 자식 클래스(Rectangle)의 생성자에서 직접 설정color를 초기화하지 않는다.constructor(color, width, height) {
super(color);
this.width = width;
this.height = height;
}
super(color)를 호출하여 color 인자를 부모 클래스의 생성자에 전달Shape)의 생성자가 color 속성을 초기화Rectangle)에서 color를 직접 설정하지 않는다.주요 차이점:
1. color 초기화 위치: 첫 번째 방식은 Rectangle 클래스에서 color를 초기화하고, 두 번째 방식은 Shape 클래스에서 초기화
2. 부모 생성자 호출 방식: 첫 번째는 인자 없이 부모 생성자를 호출하고, 두 번째는 color를 인자로 전달
3. 책임 분배: 두 번째 방식은 color 속성의 초기화 책임을 부모 클래스에 맡긴다.
어떤 방식을 선택할지는 클래스 설계와 Shape 클래스의 구현에 따라 다르다. 일반적으로 부모 클래스가 특정 속성을 관리하도록 설계되었다면 두 번째 방식이 더 적절할 수 있다. 그러나 자식 클래스에서 모든 초기화를 명시적으로 처리하고 싶다면 첫 번째 방식을 선택할 수 있다.