Get all instances of class in Javascript

You can create a static array and store it on your constructor function:

MyClass.allInstances = [];
MyClass.allInstances.push(this);

However, you need some way to figure out when to remove instances from this array, or you’ll leak memory.

Leave a Comment