0
var F15c = function (tailNumber) {
    tailNumber: this.tailNumber
    console.log(this.tailNumber);
}

F15c.prototype = {
    Ammo: {
        gun: 6,
        AMRAAM : 6,
        Sidewinder: 2,
    },
    Fuel: {
        Maintank: 10000,
        ConsumptionPerKM: 2
    },
    RangeLeft : this.Fuel.Maintank/this.Fuel.ConsumptionPerKM
}

module.exports = new F15c();

question 1: what did i do wrong in this line that 'Maintank' is undefined?

RangeLeft : this.Fuel.Maintank/this.Fuel.ConsumptionPerKM

question 2: on another script I want to create an instance of F15C with tail number 123 i've done

/*script start*/
var f15C=require('./F15C');
var F15 = new f15C(135); //error, F15C is not a constructor

so how do I do it right?

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
likuku
  • 358
  • 6
  • 21
  • 1
    Add `RangeLeft` after you define the prototype: `F15c.prototype.RangeLeft = F15c.prototype.Fuel.Maintank / F15c.prototype.Fuel.ConsumptionPerKM;` – ibrahim mahrir Dec 22 '17 at 17:55
  • Yeah, what @ibrahimmahrir said, you can't define a property that uses another property in the same definition. You'll have to do it _after_ the initial definition. – Jordan Kasper Dec 22 '17 at 17:56
  • so basically the var is reserved to the constructor, the prototype content for the initial fields and functions and any additional field and funcs that use the values should come afterwards. did i get it right? ibrahim mahrir i didnt find in the 'similar questions' a dupe for this question, although its a pretty basic one, can you please refer me to the dupe? – likuku Dec 22 '17 at 22:15
  • also, what should i do in the require part? – likuku Dec 22 '17 at 22:20

0 Answers0