无法读取属性'推送' undefined创建对象的数组属性(Javascript)

时间:2018-04-27 04:32:15

标签: javascript

我的构造函数Mash需要有一个属性是Grain对象的数组。这是我得到的,现在我的推送错误。有什么想法吗?

 function Grain(maxPPG, quantity) {
        this.maxPPG = maxPPG;
        this.quantity = quantity;


        this.calMaxPPG = function() {
            return this.maxPPG * this.quantity;
        };
    }

    let grain1 = new Grain("Pale Malt (2 Row)", 9, 37);
    let grain2 = new Grain("Caramel/Crystal Malt - 20L", .75, 35);


    function Mash(volume) {
        this.volume = volume;


        this.addGrain = function(grain) {
            this.grains.push(grain);
        }

        this.calcEOG = function() {
            let total = 0;
            this.grains.forEach(item => {
                total += item.calMaxPPG();
            });
            return total / this.volume;

        };
    }

    let mash = new Mash(7);
    mash.addGrain(grain1);
    mash.addGrain(grain2);
    console.log(mash.calcEOG());

3 个答案:

答案 0 :(得分:0)

您忘记在grains中初始化变量Mash constructor

function Mash(volume) {
    this.grains = [];
    this.volume = volume;


    this.addGrain = function(grain) {
        this.grains.push(grain);
    }

    this.calcEOG = function() {
        let total = 0;
        this.grains.forEach(item => {
            total += item.calMaxPPG();
        });
        return total / this.volume;

    };
}

修改

此外,您的Grain class实例不接受其构造函数上的字符串。我不知道你为什么要用名字实例化(我猜)。

function Grain(name, maxPPG, quantity) {
    this.name = name;
    this.maxPPG = maxPPG;
    this.quantity = quantity;


    this.calMaxPPG = function() {
        return this.maxPPG * this.quantity;
    };
}

现在你的实例会起作用。

编辑2:只是处理你的脚本:)

寻找这些建议:

// before dealing with data, work on your classes
function Grain(name, maxPPG, quantity) {
    this.name = name;
    this.maxPPG = maxPPG;
    this.quantity = quantity;
}
// and as @HMR said, always declare your class functions with prototypes
Grain.prototype.MaxPPG = function() {
    return this.maxPPG * this.quantity;
};
function Mash(volume, grains) {
    this.volume = volume;
    this.grains = grains;
}
Mash.prototype.calcEOG = function() {
    var total = 0;
    this.grains.forEach(item => {
        total += item.MaxPPG();
    });
    return total / this.volume;
};
// So after all you'll instance your classes easy.
var grain1 = new Grain("Pale Malt (2 Row)", 9, 37),
    grain2 = new Grain("Caramel/Crystal Malt - 20L", .75, 35);
var mash = new Mash(7, [grain1, grain2]); // note that litle change I did :p
console.log(mash.calcEOG());

答案 1 :(得分:0)

你还没有声明要推入的数组

答案 2 :(得分:0)

您有几个错误,使用3个参数调用构造函数,但只用2定义它。您没有初始化grans数组。

一个提示:行为可以在原型上进行。

更正后的代码如下所示:



function Grain(name,maxPPG, quantity) {//constructor takes 3 parameters when you call it
  this.name = name;//assume the first one is name
  this.maxPPG = maxPPG;
  this.quantity = quantity;
}
//you an add behavior on the prototype:
Grain.prototype.calMaxPPG = function() {
  return this.maxPPG * this.quantity;
};

let grain1 = new Grain("Name of grain", 9, 37);
let grain2 = new Grain("Name of grain", .75, 35);


function Mash(volume) {
  this.volume = volume;
  this.grains=[];//you did not initialize grains
}
//again; add behavior on the prototype
Mash.prototype.addGrain = function(grain) {
  this.grains.push(grain);
};
Mash.prototype.calcEOG = function() {
  //using reduce instead to sum up all grains
  return this.grains.reduce(
    function(all,item){
      return all + item.calMaxPPG();
    },
    0
  )/this.volume;
};
let mash = new Mash(7);
mash.addGrain(grain1);
mash.addGrain(grain2);
console.log(mash.calcEOG());