JavaScript:在方法中使用对象变量

时间:2015-05-21 20:36:36

标签: javascript object methods undefined

这里是JavaScript的新手,并且在对象引用上有点挣扎。具体来说,我正在尝试创建一个对象构造函数,它具有访问对象属性的各种方法。

对于上下文,这是计算能源使用量 1)我执行各种计算以创建作为新旧能源使用的参数传递的数组 2)我有一个Project对象构造函数来构建一个项目,其中包含将在所有对象中共享的变量。

所以我会这样称呼它:

var boilerUpgrade = new Project(oldKwh, newKwh, oldTherms, newTherms);

其中参数是值数组。

//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; //...
}
在对象内部,我还创建了许多方法,例如:

    //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];
    }
    };

但是,似乎该方法以“this.myArray”的方式引用的对象数组在没有先前计算的值的情况下显示为undefined。

我在这里缺少一个或两个关键概念,为什么对象中的方法不能使用“this.myArray”语法来访问值。关于我缺少的任何想法?

3 个答案:

答案 0 :(得分:1)

您可以将方法分配给可以访问此。*属性的原型。

像这样的东西

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

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

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

小而精彩的阅读http://javascript.crockford.com/private.html

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

答案 1 :(得分:0)

一个选项是始终将函数绑定到上下文:

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)

答案 2 :(得分:0)

感谢帮助我的尝试,特别是提供的链接。实际的问题是我的数组没有被初始化,所以当其他元素试图访问它们时,它们仍未定义。

之前,我有这种宣言:

var myArray =[];

通过像这样初始化数组来解决问题:

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