Javascript 为什么要在下面的代码中使用.payment()?
我有以下代码Javascript 为什么要在下面的代码中使用.payment()?,javascript,Javascript,我有以下代码 this.payment是汽车对象的另一个属性吗 当我想计算汽车的价格时,例如: var work_car_payments= work_car.payment(); 我为什么要使用.payment()? 付款未定义为代码中的函数。我有点困惑 function get_payment() { var the_payment = 250; the_payment += (this.seats == "leather") ? 100 : 50; the
this.payment
是汽车对象的另一个属性吗
当我想计算汽车的价格时,例如:
var work_car_payments= work_car.payment();
我为什么要使用.payment()
?
付款
未定义为代码中的函数。我有点困惑
function get_payment() {
var the_payment = 250;
the_payment += (this.seats == "leather") ? 100 : 50;
the_payment += (this.engine == "V-8") ? 150 : 75;
the_payment += (this.theradio == "CD Player") ? 35 : 10;
return the_payment;
}
function car(seats, engine, theradio) {
this.seats = seats;
this.engine = engine;
this.theradio = theradio;
this.payment = get_payment;
}
var work_car = new car("cloth", "V-6", "Tape Deck");
var fun_car = new car("leather", "V-8", "CD Player");
var custom_car = new car(fun_car.seats, work_car.engine, fun_car.theradio);
var work_car_payment = work_car.payment();
var fun_car_payment = fun_car.payment();
var custom_car_payment = custom_car.payment();
document.write("<h2>The information on the cars you requested:</h2>");
document.write("<strong>Work Car: </strong>");
document.write(work_car.seats + "," + work_car.engine + "," + work_car.theradio);
document.write("<br />");
document.write("<strong>Payments:</strong> $" + work_car_payment);
document.write("<p>");
document.write("<strong >Fun Car: </strong>");
document.write(fun_car.seats + "," + fun_car.engine + "," + fun_car.theradio);
document.write("<br />");
document.write("<strong>Payments:</strong> $" + fun_car_payment);
document.write("</p>");
document.write("<p>");
document.write("<strong>Custom Car: </strong>");
document.write(custom_car.seats + "," + custom_car.engine + ",");
document.write(custom_car.theradio);
document.write("<br />");
document.write("<strong>Payments:</strong> $" + custom_car_payment);
document.write("</p>");
函数get_payment(){
var_付款=250;
_付款+=(this.seats==“皮革”)?100:50;
_payment+=(this.engine==“V-8”)?150:75;
支付方式+=(this.theradio==“CD播放器”)?35:10;
退还已支付的费用;
}
多功能车(座椅、发动机、收音机){
这个座位=座位;
这个。发动机=发动机;
this.theradio=theradio;
this.payment=获得付款;
}
var work_car=新车(“布料”、“V-6”、“胶带组”);
var fun_car=新车(“皮革”、“V-8”、“CD播放器”);
var custom_car=新车(趣味车、座椅、工作车、发动机、趣味车、theradio);
var work_car_payment=work_car.payment();
var fun_car_payment=fun_car.payment();
var custom_car_payment=custom_car.payment();
记录。写下(“你要求的汽车信息:”);
文件。写(“工作车:”;
文件。书写(工作车座椅+”,“+工作车发动机+”,“+工作车.收音机);
文件。写(“
”);
文件。填写(“付款:$”+工作车付款);
文件。写(“”);
文档。写(“趣味车:”;
文件。书写(fun_car.seats+”、“+fun_car.engine+”、“+fun_car.theradio”);
文件。写(“
”);
文件。填写(“付款:$”+fun\u car\u付款);
文件。写(“”);
文件。写(“”);
文档。写(“定制汽车:”;
文件。书写(定制汽车座椅+”,“+定制汽车发动机+”,”;
文件。书写(定制车。无线电);
文件。写(“
”);
文件。填写(“付款:$”+定制汽车付款);
文件。写(“”);
获取付款
被定义为一个函数
function get_payment() {
var the_payment = 250;
the_payment += (this.seats == "leather") ? 100 : 50;
the_payment += (this.engine == "V-8") ? 150 : 75;
the_payment += (this.theradio == "CD Player") ? 35 : 10;
return the_payment;
}
function car(seats, engine, theradio) {
this.seats = seats;
this.engine = engine;
this.theradio = theradio;
}
car.prototype.payment = get_payment;
car()
函数包含:
this.payment = get_payment;
这将payment
属性定义为包含该函数
function get_payment() {
var the_payment = 250;
the_payment += (this.seats == "leather") ? 100 : 50;
the_payment += (this.engine == "V-8") ? 150 : 75;
the_payment += (this.theradio == "CD Player") ? 35 : 10;
return the_payment;
}
function car(seats, engine, theradio) {
this.seats = seats;
this.engine = engine;
this.theradio = theradio;
}
car.prototype.payment = get_payment;
请注意,更常见的方法是在汽车
原型中指定属性,而不是将其指定给每个对象,因为所有汽车都具有相同的功能
function get_payment() {
var the_payment = 250;
the_payment += (this.seats == "leather") ? 100 : 50;
the_payment += (this.engine == "V-8") ? 150 : 75;
the_payment += (this.theradio == "CD Player") ? 35 : 10;
return the_payment;
}
function car(seats, engine, theradio) {
this.seats = seats;
this.engine = engine;
this.theradio = theradio;
}
car.prototype.payment = get_payment;
“代码中未将付款定义为函数” — <代码>付款是
汽车
上的一个属性,涉及功能获取付款
。该函数已在代码中声明。至于“为什么”,你应该调用这个函数:明白。this.payment=get\u payment
=不管get\u payment是什么。这里,它是一个函数。尝试var get\u payment=1000000
而不是函数get_payment
(仅针对本测试对其进行注释),您将不会使用()
,并且,当您使用somecar.payment
时,您将获得1000000。