First, sorry for the lack of terminology.
If I have a constructor
function myObject(name, value){ this.name = name; this.value = value; }
and I make several objects out of it
var One = new myObject("One", 1); var Two = new myObject("Two", 2);
Is it possible to scroll through each new object from the myObject class without putting each new object into an array?
Is it possible to add an instantly calling function to the constructor that adds an object to an array?
eg.
function myObject(name, value){ this.name = name; this.value = value; this.addToArray = function(){ theArray.push(this);
Thus, any newly created objects instantly start this function and are added to the array.
Is it possible? (current syntax doesn't work, obviously)
EDIT Coming back to this in a year, I can tell you that this is possible. You simply call the function inside the constructor as follows:
function myObject(name, value){ this.name = name; this.value = value; this.addToArray = function(){ theArray.push(this); }; this.addToArray(); }
Here is an example of this in JSFIDDLE, pushing each object into an array when creating an instance, and then calling each object .speak() method directly from the array.
https://jsfiddle.net/Panomosh/8bpmrso1/