Wednesday 15 May 2013

javascript - class variable that is modified by creation of instances -


I'm interested in how to implement something in JavaScript, here's an example of Ruby:

  class person @@ count = 0 def initialize @@ count + = 1 end def self.count @@ count end end  

each time When a person starts, the counter increases.

joe = person.new person count = & gt; 1 Bob = Personian.nunepe = count = & gt; 2

How do you implement it in javascript?

You can use a property of a constructor or factory method .

with a manufacturer:

  function person () {person.count + = 1; } Person.count = 0; Var bob = new person ();  

with a factory method:

  function person () {var p = {} person.count + = 1; Return p; } Person.count = 0; Var bob = person ();  

With one of these you can also use an external variable instead of putting it on the function.


No comments:

Post a Comment