you can get instances created in a window like
var o = []; (for i in window) { if(window[i] instanceof Animal) o.push(window[i]); }
but this code does not receive instances that were not created in the window ... you can create a method on Animal to create an instance from it and save it in the collection:
+function () { var AnimalInstances = []; Animal = function (flag) { if (!flag) throw new Error("Invalid Instantiation"); }; Animal.Create = function () { var o = new Animal(true); AnimalInstances.push(o); return o; }; Animal.GetInstances = function () { return AnimalInstances; }; }();
or:
+function () { var AnimalInstances = []; Animal = function () { AnimalInstances.push(this); } Animal.GetInstances = function () { return AnimalInstances; }; }();
source share