Just declare a static property for the counter.
var Person = Class.create({
initialize: function(name) {
Person.count++;
this.name = name;
}
})
Person.count = 0;
new Person(''John''); // Person.count == 1
new Person(''Matt''); // Person.count == 2
- kangax
On Apr 13, 1:33 pm, StackOverflow
<webprogmas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hello, I''d like to have a counter for instances of my classes.
> Separately for each class, but code for counter should be installed in
> one parent class. Any ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---