Hi, all. I have class that created this way: var MyClass = Class.create(); MyClass.prototype = { myVar: {}, ...} in this class i use it as this.myVar. So, when I create two instances of this class, first instance use values from second and vice-verse. Why? As U can see it''s not singleton. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You want to setup those variables in the instance methods. The way you have it now, it''s being evaluated when the class is being created (not instantiated) and so the variable is global to all classes. var MyClass = Class.create({ initialize: function(someString){ this.myVar = someString; }, whatIsMyVar: function(){ alert( this.myVar ); } }); new MyClass(''hello'').whatIsMyVar(); new MyClass(''world'').whatIsMyVar(); -justin On Tue, May 13, 2008 at 12:24 PM, Rauan Maemirov <rauan1987-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, all. > > I have class that created this way: > > var MyClass = Class.create(); > MyClass.prototype = { > myVar: {}, > ...} > > in this class i use it as this.myVar. So, when I create two instances > of this class, first instance use values from second and vice-verse. > Why? As U can see it''s not singleton. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Because any properties/methods outside of "initialize" are shared via prototype chain among all instances of a class. - kangax On May 13, 1:24 pm, Rauan Maemirov <rauan1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, all. > > I have class that created this way: > > var MyClass = Class.create(); > MyClass.prototype = { > myVar: {}, > ...} > > in this class i use it as this.myVar. So, when I create two instances > of this class, first instance use values from second and vice-verse. > Why? As U can see it''s not singleton.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Oh. Thanks, Justin. I didn''t know that. On May 13, 11:31 pm, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You want to setup those variables in the instance methods. The way you > have it now, it''s being evaluated when the class is being created (not > instantiated) and so the variable is global to all classes. > > var MyClass = Class.create({ > initialize: function(someString){ > this.myVar = someString; > }, > whatIsMyVar: function(){ alert( this.myVar ); } > > }); > > new MyClass(''hello'').whatIsMyVar(); > new MyClass(''world'').whatIsMyVar(); > > -justin > > On Tue, May 13, 2008 at 12:24 PM, Rauan Maemirov <rauan1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, all. > > > I have class that created this way: > > > var MyClass = Class.create(); > > MyClass.prototype = { > > myVar: {}, > > ...} > > > in this class i use it as this.myVar. So, when I create two instances > > of this class, first instance use values from second and vice-verse. > > Why? As U can see it''s not singleton.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---