1

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?

1
  • 1
    How are you calling the enegiCalcs function? maybe you are loosing the scope of this at the time you call it. Commented May 21, 2015 at 20:40

3 Answers 3

1

You can assign methods to prototype that will have access to your this.* attributes.

Something like this

function Person(name) {
  this.name = name || 'Test User';
}

Person.prototype.sayHello = function() {
  alert('Hello from' + this.name);
}

var test = new Person('Bob');
test.sayHello();

Small, great reading http://javascript.crockford.com/private.html

Or here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#The_constructor

Sign up to request clarification or add additional context in comments.

Comments

0

An option would be to always bind the function to the context:

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];
    }.bind(this)

Comments

0

Appreciate the attempts to help me out, especially the links provided. The actual issue was that my arrays were not initialized so when other elements tried to access them, they were still undefined.

Before, I had this style of declaration:

var myArray =[];

The problem is fixed by initializing the arrays like so:

var myArray = [1,2,3,4];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.