-1

I have the following code.

is this.payment another property of the car object?

when i want to calculate the price of a car, for example:

var work_car_payments= work_car.payment();

why should i use .payment()? payment hasn't been defined as a function in the code. I'm a little confused.

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>");
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • _“payment hasn't been defined as a function in the code”_ — `payment` is a property on `car` referring to the function `get_payment`. That function has been declared in the code. As to “why” you should call that function: understand [how `this` works](https://stackoverflow.com/q/3127429/4642212). – Sebastian Simon Oct 18 '20 at 06:58
  • `this.payment = get_payment` = whatever get_payment is. Here, it's a function. try `var get_payment = 1000000;` instead of `function get_payment` (comment it out just for this test), and you'll not be about to use `()`, and, when you use `somecar.payment` you'll get 1000000. – iAmOren Oct 18 '20 at 07:00

1 Answers1

1

get_payment is defined as a function.

The car() function contains:

this.payment = get_payment;

This defines the payment property as containing that function.

Note that the more common way to do this would be to assign the property in the car prototype, rather than assigning it to each object, since all cars get the same function.

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;
Barmar
  • 741,623
  • 53
  • 500
  • 612