Javascript 我总是得到一个';未捕获范围';我的代码中有错误,我可以';我不明白为什么

Javascript 我总是得到一个';未捕获范围';我的代码中有错误,我可以';我不明白为什么,javascript,html,ecmascript-6,Javascript,Html,Ecmascript 6,我的导师让我创建一个代表车辆的类和两个子类,分别代表“救护车”和“公共汽车”。我的HTML文件将实例化每个子类,并允许我驱动它们 通过发行方式。在我写这篇文章时,我的控制台中不断出现“未捕获范围”错误 class车辆{ 构造器(颜色、方向、当前速度、最高速度){ this.color=color;//字符串 this.direction=direction;//整数0-359(表示指南针) this.currentSpeed=currentSpeed;//整数 this.topSpeed=to

我的导师让我创建一个代表车辆的类和两个子类,分别代表“救护车”和“公共汽车”。我的HTML文件将实例化每个子类,并允许我驱动它们 通过发行方式。在我写这篇文章时,我的控制台中不断出现“未捕获范围”错误

class车辆{
构造器(颜色、方向、当前速度、最高速度){
this.color=color;//字符串
this.direction=direction;//整数0-359(表示指南针)
this.currentSpeed=currentSpeed;//整数
this.topSpeed=topSpeed;//整数
this.engineStarted=true;//布尔值
}
//方法:
打开(){
this.engineStarted=true;
}
信息(){
如果(此.engineStarted){
const info=`${this.color},${this.direction},${this.currentSpeed},${this.topSpeed}`;
退货信息;
}否则{
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活。”;
返回状态;
}
}
statusOn(){
如果(此.engineStarted){
const statusOn=“发动机启动,车辆运行。”;
返回状态ON;
}否则{
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活。”;
返回状态;
}
}
关闭(){
this.engineStarted=false;
}
信息(){
const status=“发动机现在已断开,车辆处于非活动状态。”
返回状态;
}
加速{
如果(this.engineStarted=false){
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活”;
返回状态;
}
如果(该电流速度<100){
该电流速度+=10;
log(“现在加速速度为:+此.currentSpeed”);
}否则{
console.log(“达到最高速度”);
} 
}
刹车(){
如果(this.engineStarted=true){
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活”;
返回状态;
}
如果(此.currentSpeed>10){
该电流速度-=10;
console.log(“现在制动速度:”+此.currentSpeed);
}否则{
该电流速度=0;
log(“速度现在为:+this.currentSpeed”);
}
}
左转{
如果(this.engineStarted=true){
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活”;
返回状态;
}
这个方向是-90;
如果(此方向<0){
这个方向+90;
}
}
右转{
如果(this.engineStarted=true){
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活”;
返回状态;
}
这个方向+90;
如果(此方向>359){
这个方向是-90;
}
}
}
班车延伸到汽车{
构造器(颜色、方向、当前速度、最高速度、座位数){
超级(颜色、方向、当前速度、最高速度);
this.numberOfSeats=numberOfSeats;
}
信息(){
如果(此.engineStarted){
const info=`${this.color},${this.direction},${this.currentSpeed},${this.topSpeed},${this.numberOfSeats}seats`;
退货信息;
}否则{
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活”;
返回状态;
}
}
设置座位数(新座位){
if(newSeats<50){
this.numberOfSeats=newSeats;
}否则{
警报(“超过座位号”);
}
}
}
二级救护车{
构造器(颜色、方向、当前速度、最高速度、警报器){
超级(颜色、方向、当前速度、最高速度、警报器);
这个。警报器=警报器;
}
信息(){
如果(此.engineStarted){
const info=`${this.color}.${this.direction},${this.currentSpeed},${this.topSpeed},切换${this.sirens}`;
退货信息;
}否则{
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活”;
返回状态;
}
}
切换警报器(){
这是真的;
}
设置警报器(切换警报器){
如果(这是警报声){
const info=“警报已激活”;
退货信息;
}否则{
const status=“警报器未激活”;
返回状态;
}
}
}

车辆
let bus=新公交车(“黄色”、90、45、50、45);
let救护车=新救护车(“白色”、180、60、65);
警报(bus.info());
警报(救护车信息());
您的设定者在此:

set numberOfSeats(newSeats) {
  if (newSeats < 50) {
    this.numberOfSeats = newSeats;
  } else {
    alert("Exceeded Seat Number");
  }
}


但是,您并没有真正解释为什么首先要为此属性使用setter,因此可能还有其他合适的解决方案。

如@jfriend00所说,setter上有一个无限循环。 我认为一个不同的解决方案可以是这个

class Bus extends Vehicle {
  constructor(color, direction, currentSpeed, topSpeed, numberOfSeats) {
    super(color, direction, currentSpeed, topSpeed);
    this.setseats(numberOfSeats);
  }
  info() {
    if (this.engineStarted) {
      const info = `${this.color}, ${this.direction}, ${this.currentSpeed}, ${this.topSpeed}, ${this.numberOfSeats} seats`;
      return info;
      } else {
      const status = "Engine has not been started! Vehicle is idle and inactive. Please activate";
      return status;
      }
    }

    setseats(newSeats){
        if (newSeats < 50) {
        this.numberOfSeats = newSeats;
      } else {
        alert("Exceeded Seat Number");
      }
    }

   } 
class总线扩展车辆{
构造器(颜色、方向、当前速度、最高速度、座位数){
超级(颜色、方向、当前速度、最高速度);
此.设置座椅(座椅数量);
}
信息(){
如果(此.engineStarted){
const info=`${this.color},${this.direction},${this.currentSpeed},${this.topSpeed},${this.numberOfSeats}seats`;
退货信息;
}否则{
const status=“发动机尚未启动!车辆处于怠速和非活动状态。请激活”;
返回状态;
}
}
集合吃(新闻吃){
if(newSeats<50){
this.numberOfSeats=newSeats;
}否则{
警报(“超过座位号”);
}
}
} 

对于起动机,
车辆
类具有重复的
信息
方法。如果代码有适当的缩进,那么它将更容易理解
class Bus extends Vehicle {
  constructor(color, direction, currentSpeed, topSpeed, numberOfSeats) {
    super(color, direction, currentSpeed, topSpeed);
    this.setseats(numberOfSeats);
  }
  info() {
    if (this.engineStarted) {
      const info = `${this.color}, ${this.direction}, ${this.currentSpeed}, ${this.topSpeed}, ${this.numberOfSeats} seats`;
      return info;
      } else {
      const status = "Engine has not been started! Vehicle is idle and inactive. Please activate";
      return status;
      }
    }

    setseats(newSeats){
        if (newSeats < 50) {
        this.numberOfSeats = newSeats;
      } else {
        alert("Exceeded Seat Number");
      }
    }

   }