Reputation: 59
For instance, let's say I'm really hungry so I just keep making pancakes!
var Buttermilk = new Pancake("Buttermilk", "Delicious");
var ChocolateChip = new Pancake("Chocolate Chip", "Amazing");
var BlueBerry = new Pancake("Blue Berry", "The Best");
var SnozBerry = new Pancake("Snoz Berry", "What's a Snoz Berry?");
How would I count how many pancakes I just made without manually doing it? Is there a code that says "There are this many variables that are of the Pancake variety"?
EDIT:
Thank you for the answers! I was specifically looking for a simple way to quickly count the amount of times I created an object with a small amount of code. And that is what I got, thank you!
Upvotes: 4
Views: 4704
Reputation: 2570
a more object oriented approach would be to use a static method an a static property. although JS doesn't have static property we can set it on constructor itself e.g.
class Pancake {
static count() {
Pancake.counter = (Pancake.counter || 0) + 1;
return;
}
constructor(objName) {
Pancake.count();
this.name = objName;
}
}
new Pancake("A");
console.log(Pancake.counter); //1
new Pancake("B");
console.log(Pancake.counter); //2
new Pancake("C");
console.log(Pancake.counter); //3
new Pancake("D");
console.log(Pancake.counter); //4
new Pancake("E");
console.log(Pancake.counter); //5
demo : https://jsfiddle.net/mux2qnsc/
Upvotes: 0
Reputation: 848
If you want to count the number of instances created from a prototype you need a property like:
Asset.prototype.index = 0;
Now, in the constructor itself use:
function Asset () {
this.index = Asset.prototype.index++;
}
Upvotes: 0
Reputation: 4354
I realise you've already accepted an answer but this took me a while! From your question I was thinking you may not want to change the Pancake class. So here's an attempt to avoid that.
This function will search all the objects in the object you specify and count all the instances of your type.
// Usage:
searchObjectForType(window, Pancake); // returns a number.
function searchObjectForType(obj,type){
// Keep track of objects in stack so we don't overflow by searching into the same objects;
var stackObjs = [];
function recursiveProbe(obj){
var foundCount = 0;
var objType;
// Some types will throw (iframes/nulls/..)
try{
objType = obj.toString();
}
catch(err){
return 0;
}
// Skip document/previous objects as we know they won't have any.
if(typeof obj === "string" || stackObjs.indexOf(objType)>=0 || obj===document){
return 0;
}
else{
stackObjs.push(objType);
}
for (var i in obj){
var prop = obj[i];
if(prop instanceof type){
foundCount++;
}
else{
foundCount += recursiveProbe(prop);
}
}
// Remove this type from stackObjs so we can search future types.
stackObjs.splice(stackObjs.indexOf(obj.toString()),1);
return foundCount;
}
return recursiveProbe(obj);
}
I'm sure there are cases where this fails, so feedback appreciated!
Upvotes: 0
Reputation: 4860
You can have static properties in javascript classes. You can either hide them in closures that way:
var Pancake = (function() {
var instances = 0;
return function(a, b) {
this.a = a;
this.b = b;
instances++;
Pancake.prototype.instances = function() { // equivalent of a static method
return instances;
}
};
}());
or put them in the object prototype:
var pancake = function(a, b) {
this.a = a;
this.b = b;
pancake.prototype.count = pancake.prototype.count ? pancake.prototype.count + 1 : 1; // equivalent of a static property
}
You can also "override" the constructor, by implementing some kind of "inheritance", such as in this fiddle:
var Pancake = function(a, b) {
this.a = a;
this.b = b;
};
var before = Pancake.prototype;
var Pancake = function() {
console.log("overriden");
Pancake.prototype.instances = Pancake.prototype.instances ? Pancake.prototype.instances + 1 : 1; // here you should restore the whole prototype
return before.constructor.apply(this, arguments);
};
var a = new Pancake("a", "b");
document.write(Pancake.prototype.instances + "<br />");
var b = new Pancake("c", "d");
document.write(Pancake.prototype.instances + "<br />");
document.write(JSON.stringify(a) + "<br />");
document.write(JSON.stringify(b) + "<br />");
Upvotes: 5
Reputation: 3735
You can keep a counter which will get increment in constructor, here is a good solution
How can I count the instances of an object?
Upvotes: 2
Reputation: 558
Use a counter variable inside the Pancake function.. :)
var count = 0;
function Pancake(){
// Cook pancakes
count += 1;
}
console.log('Total pancakes' + count);
Upvotes: 1