gpt4 book ai didi

javascript - 为什么这个脚本不起作用?实时预览中不显示任何内容

转载 作者:行者123 更新时间:2023-11-28 03:23:49 24 4
gpt4 key购买 nike

我正在尝试创建一个具有某些值的汽车项目,但是当我运行程序时,函数 ShowCar 没有显示任何内容...

    <script>    
function Car(model,price,topspeed,acceleration,consumption) {
this.model=model;
this.price=price;
this.topspeed=topspeed;
this.acceleration=acceleration;
this.consumption=consumption;
}

function ShowCar() {
document.write("Model:"+this.model+"<br>");
document.write("Price:"+this.price+"<br>");
document.write("Topspeed:"+this.topspeed+"<br>");
document.write("Acceleration:"+this.acceleration+"<br>");
document.write("Average Consumption:"+this.consumption+"<hr>");
}
</script>
</head>
<body>
<h1> Car List </h1>
<script>
Car1=new Car("Seat Ibiza","6.500 euros","190 km/h","9.7 s","5.3 l/100km");
Car1.ShowCar();
</script>
</body>

最佳答案

我强烈建议阅读 javascript 中的 OOP:https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS

但是对于您的问题,这里有一个可行的解决方案:

function Car(model, price, topspeed, acceleration, consumption) {
this.model = model;
this.price = price;
this.topspeed = topspeed;
this.acceleration = acceleration;
this.consumption = consumption;

this.ShowCar = function() {
document.write("Model:" + this.model + "<br>");
document.write("Price:" + this.price + "<br>");
document.write("Topspeed:" + this.topspeed + "<br>");
document.write("Acceleration:" + this.acceleration + "<br>");
document.write("Average Consumption:" + this.consumption + "<hr>");
}
}



var Car1 = new Car("Seat Ibiza", "6.500 euros", "190 km/h", "9.7 s", "5.3 l/100km");
Car1.ShowCar();
<h1>Car List </h1>

关于javascript - 为什么这个脚本不起作用?实时预览中不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58858679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com