Ark13th
Ark13th

Reputation: 23

Why JS constructor method works fine when called within constructor but does not execute well when self invoked / IIEF?

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));

Upvotes: 0

Views: 42

Answers (0)

Related Questions