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

JavaScript:在方法中使用对象变量,javascript,object,methods,undefined,Javascript,Object,Methods,Undefined,这里是JavaScript新手,在对象引用方面有点困难。具体地说,我正在尝试创建一个对象构造函数,它具有访问对象属性的各种方法 在上下文中,这是为了计算能源使用量。 1) 我进行了各种计算,以创建作为新旧能源使用参数传递的数组 2) 我有一个项目对象构造函数来构建一个项目,其中的变量将在所有对象中共享 所以我会这样称呼它: var boilerUpgrade = new Project(oldKwh, newKwh, oldTherms, newTherms); 其中参数是值的数组 //bui

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

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

所以我会这样称呼它:

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];
    }
    };
//创建计算MMBtu节省的方法
this.energyCalcs=函数(){

对于(var i=0;i而言,一个选项是始终将函数绑定到上下文:

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)
this.energyCalcs=function(){

对于(var i=0;i,您可以将方法分配给原型,该原型将访问您的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();
小而好的阅读


或者在此

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

以前,我有过这样的声明:

var myArray =[];
通过如下方式初始化阵列解决了此问题:

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

您如何调用eLogiccs函数?在调用它时,可能正在失去它的作用域。