0

Case 1: discount works well.

function Car (plate,make,model,price,color)  { 
    this.carPrice = price;   
    this.disc = function () {
         return (this.carPrice > 20000 ? this.carPrice * .75 :
             this.carPrice < 5000 ? this.carPrice * .9 : this.carPrice * .85);
     };
     this.discount = this.disc();
}

console.log(new Car('test', 'test', 'test', 10000, null));

Case 2: discount renders NaN. why?

function Car (plate,make,model,price,color)  { 
    this.carPrice = price;   
    this.discount = function () {
         return (this.carPrice > 20000 ? this.carPrice * .75 :
             this.carPrice < 5000 ? this.carPrice * .9 : this.carPrice * .85);
     }();
}

console.log(new Car('test', 'test', 'test', 10000, null));
David
  • 208,112
  • 36
  • 198
  • 279
Ark13th
  • 13
  • 4
  • Why is there a function at all? `this.discount = price * (price > 20000 ? 0.75 : (price < 5000 ? 0.9 : 0.85))` – Andreas Feb 09 '21 at 13:35
  • calling it as an IIFE means that you don't have a value for `this` set, so it's not the current instance being created. – VLAZ Feb 09 '21 at 13:44

0 Answers0