New to JavaScript here and struggling a bit with object references. Specifically, I'm trying to create an object constructor that has various methods that access the object properties.
For context, this is to calculate energy usage.
1) I preform various calculations to create the arrays that are passed as arguments for both the old and new energy usage
2) I have a Project object constructor to build a project with the variables that will be shared within all objects.
So I would call it like this:
var boilerUpgrade = new Project(oldKwh, newKwh, oldTherms, newTherms);
where the arguments are arrays of values.
//build an object constructor for each project
function Project(oldKwh, newKwh, oldTherms, newTherms){
//create arrays for the old & new energy usages
this.oldKwh = oldKwh;
this.newKwh = newKwh;
this.kwhSaved = [];
this.totalKwhSaved;
this.oldTherms = oldTherms;
this.newTherms = newTherms;
this.thermsSaved = [];
this.totalThermsSaved;
this.oldMMBtu = [];
this.newMMBtu = [];
this.MMBtuSaved = [];
this.totalMMBtuSaved;
this.electricCostSaved = [];
this.totalElectricCostSaved;
this.thermsCostSaved = [];
this.totalThermsCostSaved;
this.costSaved = [];
this.totalCostSaved; //...
}
within the object, I also create a number of methods, such as:
//create method to calculate MMBtu savings
this.energyCalcs = function(){
for (var i =0; i<12; i++){
this.oldMMBtu[i] = (this.oldKwh[i]*(3412.3/1000000)) + (this.oldTherms[i]*(1000/1000000));
this.newMMBtu[i] = (this.newKwh[i]*(3412.3/1000000)) + (this.newTherms[i]*(1000/1000000));
this.thermsSaved[i] = this.oldTherms[i] - this.newTherms[i];
this.kwhSaved[i] = this.oldKwh[i] - this.newKwh[i];
this.MMBtuSaved[i] = this.oldMMBtu[i]-this.newMMBtu[i];
this.totalThermsSaved += this.thermsSaved[i];
this.totalKwhSaved += this.kwhSaved[i];
this.totalMMBtuSaved += this.MMBtuSaved[i];
}
};
However, it seems that the arrays from the object that are referenced by the method in the manner of "this.myArray" show up as undefined without the values previously calculated.
I'm missing a key concept or two here as to why a method within an object can't use the "this.myArray" syntax to access values. Any ideas on what I'm missing?