상속을 이용해 새로운 행동 얻기

class Bicycle {
  constructor({ size, tapeColor }) {
    this.size = size;
    this.tapeColor = tapeColor;
  }

  getSpares() {
    return {
      chain: "10-speed",
      tireSize: "23",
      tapeColor: this.tapeColor,
    };
  }
}

const bike = new Bicycle({ size: "M", tapeColor: "red" });
console.log(bike.size); // 'M'
console.log(bike.getSpares()); // { chain: '10-speed', tireSize: '23', tapeColor: 'red' }
class Bicycle {
  constructor({ size, tapeColor, style, frontShock, rearShock }) {
    this.size = size;
    this.tapeColor = tapeColor;
    this.style = style;
    this.frontShock = frontShock;
    this.rearShock = rearShock;
  }

  getSpares() {
    if (this.style === "road") {
      return {
        chain: "10-speed",
        tireSize: "23", // 밀리미터
        tapeColor: this.tapeColor,
      };
    }

    return {
      chain: "10-speed",
      tireSize: "2.1", // 인치
      tapeColor: this.tapeColor,
    };
  }
}

const bike = new Bicycle({
  size: "S",
  tapeColor: "red",
  style: "mountain",
  frontShock: "Manitou",
  rearShock: "Fox",
});

console.log(bike.getSpares()); // { chain: '10-speed', tireSize: '2.1', tapeColor: 'red' }
class MountainBike extends Bicycle {
  constructor(props = {}) {
    super(props);
    this.frontShock = props.frontShock;
    this.rearShock = props.rearShock;
  }

  getSpares() {
    return {
      ...super.getSpares(),
      rearShock: this.rearShock,
      frontShock: this.frontShock,
    };
  }
}

const mountainBike = new MountainBike({
  size: "S",
  frontShock: "Manitou",
  rearShock: "Fox",
});
console.log(mountainBike.size); // S
console.log(mountainBike.getSpares());
/**
 * {
 *   chain: '10-speed',
 *   tireSize: '23', // 타이어 사이즈가 기대한것과 다름
 *   tapeColor: undefined, // 해당 사항 없음
 *   rearShock: 'Fox',
 *   frontShock: 'Manitou'
 * }
 */