Monday, 3 April 2017

Object variable is undefined even though it's initialised

I have a Node project where I create a Unit and an AddGate:

var Unit = function(value, weight) {
    this.value = value;
    this.weight = weight;
}

var AddGate = function() {
    this.sum_function = function(units) { 
        sum = 0;
        for (unit in units)
            sum += unit.value;
        return sum;
    };
};

AddGate.prototype = {
    forward: function(units) {
        this.units = units;
        this.output_unit = new Unit(this.sum_function(units), 0.0);
        return this.output_unit;
    }
}

I create some Units, an AddGate, and a ForwardNeuron (guess what I'm making):

var in_1 = new Unit(1.0, 0.0);
...
var in_9 = new Unit(3.0, 0.0);

var add = new AddGate();

var forwardNeuron = function() {
    a = add.forward({in_1, in_2, in_3, in_4, in_5, in_6, in_7, in_8, in_9});
};
forwardNeuron();

But for some reason, when in sum_function of AddGate, I can access each unit of units fine, but when I try to access unit.value, it says it's undefined, even though I've clearly initialised it. Am I missing something?



via Daniel Soutar

No comments:

Post a Comment