0

This is what i have so far. Basically every time i make a new Item object i need it to create a specific number of another object called Sensor. I've tried a few other methods and none seem to way the work that i need.

function Item (id,number,operator,numOfSensors){
    this.id = id;
    this.number = number;
    this.operator = operator;
    this.numOfSensors = numOfSensors;
    var sensor = new Sensor[numOfSensors];

}

var Sensor = {
    timeStamp:[],

    itemPassed: function(){
        timeStamp.push(Date.now());
    }

}

Thanks so much for any help :) i'm a bit new to js EDIT:

Hey guys! Thanks for the help. Basically my issue is neatly making an array of Objects. In the item object i want to make a [numOfSensors] amount of the Sensor object. I cant seem to find a way to do that. SO i guess my question is how would i go about creating an array of objects with a set number of elements?

  • Welcome to SO. What specifically is the issue? – J0e3gan Aug 27 '15 at 14:37
  • Hard grasping exactly what you want. It seems you may have forgotten to ask a question. – kemicofa ghost Aug 27 '15 at 14:43
  • Hey guys! Thanks for the help. Basically my issue is neatly making an array of Objects. In the item object i want to make a [numOfSensors] amount of the Sensor object. I cant seem to find a way to do that. SO i guess my question is how would i go about creating an array of objects with a set number of elements? – Arin Yaldizciyan Aug 27 '15 at 14:47
  • @ArinYaldizciyan usually "array of objects" refers to a structure like this: `[ {}, {}, {} ]` but I'm not following what you want to do? – Daniel Lizik Aug 27 '15 at 15:03
  • Sorry, ill try to explain better. Basically when i make a new Item object. You'll notice i have a line within the item object that says var sensor = new Sensor[3]; i want the Item object to make 3 New sensor objects. The thing is the way i've written it that doesnt seem to work. How would you guys create an array of objects? Sorry if that's not a good way of explaining it – Arin Yaldizciyan Aug 27 '15 at 15:09
  • You really have to learn basics. You're talking about `Item` object, but it's a function, then you're using `Sensor` object as it was a function ... Please add all the relevant information from comments to your question, nobody is going to dig it out from a long comment thread. – Teemu Aug 27 '15 at 15:57

1 Answers1

0

Not sure when you really want to call itemPassed() and whether this is not rather a function of Item, but here is some code that might get you get started:

var Item = function (id, number, operator, numOfSensors) {
    this.id = id;
    this.number = number;
    this.operator = operator;
    this.numOfSensors = numOfSensors;
    var sensorArray = [];
    for (var i = 0; i<numOfSensors; i++) {
        sensorArray.push(new Sensor());
    }
}

var Sensor = function() {
    this.timeStamps = [];
}

Sensor.prototype.itemPassed = function(){
    this.timeStamps.push(Date.now());
}

See:

Community
  • 1
  • 1
Risadinha
  • 16,058
  • 2
  • 88
  • 91