我是使用类的新手,我试图弄清楚如何使信息显示在浏览器中。我已经制作了一个JS类(车辆),并试图使用它通过对象的解构在浏览器中显示信息。我无法弄清楚我在这里做错了什么,因为在浏览器中什么也没有,但是在检查页面元素时却没有弹出错误。谁能指出我正确的方向?
这里是HTML:
<body>
<div id="displays">
</div>
<script src="vehicle.js">
let car = new Vehicle("black", 137, 127, 208);
document.getElementById('displays').innerHTML = car.show();
</script>
</body>
这是JS:
class Vehicle{
constructor(color, direction, currentSpeed, topSpeed, engineStarted){
this._color = color;
this._direction = direction;
this._currentSpeed = currentSpeed;
this._topSpeed = topSpeed;
this._engineStarted = true;
this.active = true;
}
show() {
if(this._active)
{
const stats = `${this._color} car, Top Speed: ${this._topSpeed} mph, Current Speed: ${this._currentSpeed} mph, ${this._direction}`;
return stats;
}
}