Javascript 为什么这个脚本不起作用?实时预览中不显示任何内容
我正在尝试创建一个带有一些值的汽车项目,但是当我运行程序时,函数ShowCar没有显示任何内容Javascript 为什么这个脚本不起作用?实时预览中不显示任何内容,javascript,html,Javascript,Html,我正在尝试创建一个带有一些值的汽车项目,但是当我运行程序时,函数ShowCar没有显示任何内容 <script> function Car(model,price,topspeed,acceleration,consumption) { this.model=model; this.price=price; this.topspeed=topspeed; t
<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>
功能车(型号、价格、最高速度、加速度、消耗){
这个模型=模型;
这个。价格=价格;
这个。最高速度=最高速度;
这个。加速度=加速度;
这个.消费=消费,;
}
功能展示车(){
文档。写入(“模型:“+this.Model+”
”;
文件。写入(“价格:“+this.Price+”
”);
文件。写入(“Topspeed:+this.Topspeed+”
”;
文档。写入(“加速度:“+this.Acceleration+”
”;
文档。写入(“平均消耗量:“+this.Consumption+”
”;
}
汽车清单
Car1=新车(“座椅伊维萨”、“6.500欧元”、“190公里/小时”、“9.7秒”、“5.3升/100公里”);
Car1.ShowCar();
Car.prototype.ShowCar=function(){
文档。写入(“模型:“+this.Model+”
”;
文件。写入(“价格:“+this.Price+”
”);
文件。写入(“Topspeed:+this.Topspeed+”
”;
文档。写入(“加速度:“+this.Acceleration+”
”;
文档。写入(“平均消耗量:“+this.Consumption+”
”;
}
您可以将ShowCar设置为Car原型的一部分,这样就可以从Car初始化的对象调用它。但是,我建议您学习一些ES6并使用类来代替。Car.prototype.ShowCar=function(){
文档。写入(“模型:“+this.Model+”
”;
文件。写入(“价格:“+this.Price+”
”);
文件。写入(“Topspeed:+this.Topspeed+”
”;
文档。写入(“加速度:“+this.Acceleration+”
”;
文档。写入(“平均消耗量:“+this.Consumption+”
”;
}
您可以将ShowCar设置为Car原型的一部分,这样就可以从Car初始化的对象调用它。不过,我建议您学习一些ES6,改用一门课。您应该将ShowCar改为:
function ShowCar(car) {
document.write("Model:"+car.model+"<br>");
document.write("Price:"+car.price+"<br>");
document.write("Topspeed:"+car.topspeed+"<br>");
document.write("Acceleration:"+car.acceleration+"<br>");
document.write("Average Consumption:"+car.consumption+"<hr>");
}
功能展示车(车){
文档。写入(“模型:“+car.Model+”
”;
文档。写入(“价格:“+car.Price+”
”);
文件。写入(“Topspeed:+car.Topspeed+”
”;
文档。写入(“加速度:“+car.Acceleration+”
”;
文件。填写(“平均消耗量:+汽车消耗量+”
”;
}
然后调用ShowCar(Car1)。ShowCar目前是一个独立的功能,而不是汽车的一种方法。您应该将ShowCar更改为:
function ShowCar(car) {
document.write("Model:"+car.model+"<br>");
document.write("Price:"+car.price+"<br>");
document.write("Topspeed:"+car.topspeed+"<br>");
document.write("Acceleration:"+car.acceleration+"<br>");
document.write("Average Consumption:"+car.consumption+"<hr>");
}
功能展示车(车){
文档。写入(“模型:“+car.Model+”
”;
文档。写入(“价格:“+car.Price+”
”);
文件。写入(“Topspeed:+car.Topspeed+”
”;
文档。写入(“加速度:“+car.Acceleration+”
”;
文件。填写(“平均消耗量:+汽车消耗量+”
”;
}
然后调用ShowCar(Car1)。ShowCar目前是一个独立的功能,而不是一种汽车方法。在您的功能汽车中,“this”指的是什么?如果要定义javascript类,请参阅
请使用浏览器的调试器查看发生了什么:)功能车中的“this”指的是什么?如果要定义javascript类,请参阅
请使用浏览器的调试器查看发生的情况:)如果需要面向对象编程,请编写适当的类:
class Car {
constructor(model, price, topspeed, acceleration, consumption) {
this.model = model;
this.price = price;
this.topspeed = topspeed;
this.acceleration = acceleration;
this.consumption = consumption;
}
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>");
}
}
如果需要面向对象编程,请编写适当的类:
class Car {
constructor(model, price, topspeed, acceleration, consumption) {
this.model = model;
this.price = price;
this.topspeed = topspeed;
this.acceleration = acceleration;
this.consumption = consumption;
}
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>");
}
}
我强烈建议阅读javascript中的OOP: 但对于您的问题,这里有一个有效的解决方案:
功能车(型号、价格、最高速度、加速度、消耗){
this.model=模型;
这个价格=价格;
this.topspeed=最高速度;
这个。加速度=加速度;
这个。消费=消费;
this.ShowCar=函数(){
文档。写入(“模型:“+this.Model+”
”;
文件。写入(“价格:+this.Price+”
”;
文档。写入(“Topspeed:+this.Topspeed+”
”;
文档。写入(“加速度:+this.Acceleration+”
”;
记录。填写(“平均消耗量:“+this.Consumption+”
”;
}
}
var Car1=新车(“座椅伊维萨”、“6.500欧元”、“190公里/小时”、“9.7秒”、“5.3升/100公里”);
Car1.ShowCar()代码>
汽车列表
我强烈建议阅读javascript中的OOP:
但对于您的问题,这里有一个有效的解决方案:
功能车(型号、价格、最高速度、加速度、消耗){
this.model=模型;
这个价格=价格;
this.topspeed=最高速度;
这个。加速度=加速度;
这个。消费=消费;
this.ShowCar=函数(){
文档。写入(“模型:“+this.Model+”
”;
文件。写入(“价格:+this.Price+”
”;
文档。写入(“Topspeed:+this.Topspeed+”
”;
文档。写入(“加速度:+this.Acceleration+”
”;
记录。填写(“平均消耗量:“+this.Consumption+”
”;
}
}
var Car1=新车(“座椅伊维萨”、“6.500欧元”、“190公里/小时”、“9.7秒”、“5.3升/100公里”);
Car1.ShowCar()代码>
车辆列表
ShowCar()
不是车辆
的成员。。。所以它不起作用。在浏览器中打开开发控制台,将显示一个关于此的红色错误。将函数移动到Car
函数,因为this.ShowCar=function(){//code}
这是否回答了您的问题?使用Car.prototype.ShowCar=function(){…
,ShowCar()代替function ShowCar(){…
)
不是Car
的成员,因此它不起作用。在浏览器中打开开发控制台,将显示一个关于该错误的红色错误。将函数移动到Car
函数,因为this.ShowCar=function(){//code}
这是否回答了您的问题?而不是function ShowCar(){…
,使用Car.prototype.ShowCar=function(){…
。