【问题标题】:Why is my console is say NaN is class extend Javascript为什么我的控制台说 NaN 是类扩展 Javascript
【发布时间】:2022-11-28 17:07:59
【问题描述】:

所以我正在制作项目,当我尝试从父类获取参数时,它说 NaN,而另一个是 true。 这里的代码:

class transportasi {//class parent
  constructor(nama,roda,pintu){
  this.nama = nama
  this.roda = roda
  this.pintu = pintu

  }
}

class mobil extends transportasi{//Class Children
  constructor(roda,lampu){
    super(roda)//the problem
    this.lampu = lampu
  }

  jmlahfeature(){
    return this.lampu + this.roda 
  }
}
const mobil1 = new mobil(2,4)//the problem

//I cant fill the value of roda only lampu
console.log("Hasil Perhitungan Feature mobil : " + mobil1.jmlahfeature())

我想要它所以我可以填充参数roda的值。所以它不会在控制台中说NaN

【问题讨论】:

  • rodatransportasi 构造函数的第二个参数。您只将 1 个参数传递给super()

标签: javascript html


【解决方案1】:

正如 Ivar 正确指出的那样,roda 是第二个参数,您只传递 super() 中的第一个参数。

以下代码应该会给出您想要的结果。

class transportasi {//class parent
    constructor(nama,roda,pintu){
    this.nama = nama
    this.roda = roda
    this.pintu = pintu
    
    }
}

class mobil extends transportasi{//Class Children
    constructor(roda,lampu){
        super('here should be some value', roda) 
        this.lampu = lampu
    }
    
    jmlahfeature(){
        return this.lampu + this.roda 
    }
}
const mobil1 = new mobil(2,4)//the problem

//I cant fill the value of roda only lampu
console.log("Hasil Perhitungan Feature mobil : " + mobil1.jmlahfeature())

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    相关资源
    最近更新 更多