I''m obviously a bit slow - I''m afraid I can''t get the below code to work as expected. I''ve changed the concepts to family, people & toys to simplify it, but no luck. I would expect the code to make two alerts: - My name is John. My toys are: camera,ipod, - My name is Jane. My toys are: iphone,computer, However, while the peoples'' names are listed appropriately, it gives all the toys to both people: - My name is John. My toys are: camera,ipod,iphone,computer, - My name is Jane. My toys are: camera,ipod,iphone,computer, Why is this? Many thanks, Iain ------------------------------------------------------------------------------------------------------ var Toy = Class.create({ initialize:function(name) { this.name = name; } }); var Person = Class.create({ toys: [], initialize: function(name) { this.name = name; }, speak: function() { var string = "My name is " + this.name + ". My toys are: "; $A(this.toys).each(function (toy) { string += toy.name + ","; }.bind(this)); alert(string); }, appendToys: function(items) { $A(items).each(function (item) { this.toys.push(new Toy(item)); }.bind(this)); } }); var Family = Class.create({ people:[], appendPerson: function(name) { this.people.push(new Person(name)); return this.people.last(); }, converse: function() { $A(this.people).each(function(person) { person.speak(); }); } }); var family = new Family(); var p1 = family.appendPerson(''John''); p1.appendToys([ ''camera'', ''ipod'' ]); var p2 = family.appendPerson(''Jane''); p2.appendToys([ ''iphone'', ''computer'' ]); family.converse(); --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ityndall-ue636x8T32g@public.gmane.org
2008-Jan-10 19:49 UTC
Re: Classes & Objects - simple problem
You need to move "toys: []" in your person class to the initialize method: var Person = Class.create({ initialize: function(name) { this.name = name; this.toys = []; }, speak: function() { var string = "My name is " + this.name + ". My toys are: "; $A(this.toys).each(function (toy) { string += toy.name + ","; }.bind(this)); alert(string); }, appendToys: function(items) { $A(items).each(function (item) { this.toys.push(new Toy(item)); }.bind(this)); } }); On Jan 10, 2:11 pm, iporter <ispor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m obviously a bit slow - I''m afraid I can''t get the below code to > work as expected. I''ve changed the concepts to family, people & toys > to simplify it, but no luck. I would expect the code to make two > alerts: > > - My name is John. My toys are: camera,ipod, > - My name is Jane. My toys are: iphone,computer, > > However, while the peoples'' names are listed appropriately, it gives > all the toys to both people: > > - My name is John. My toys are: camera,ipod,iphone,computer, > - My name is Jane. My toys are: camera,ipod,iphone,computer, > > Why is this? Many thanks, > Iain > > ------------------------------------------------------------------------------------------------------ > var Toy = Class.create({ > initialize:function(name) { > this.name = name; > } > > }); > > var Person = Class.create({ > toys: [], > initialize: function(name) { > this.name = name; > }, > speak: function() { > var string = "My name is " + this.name + ". My toys are: "; > $A(this.toys).each(function (toy) { > string += toy.name + ","; > }.bind(this)); > > alert(string); > }, > appendToys: function(items) { > $A(items).each(function (item) { > this.toys.push(new Toy(item)); > }.bind(this)); > }}); > > var Family = Class.create({ > people:[], > appendPerson: function(name) { > this.people.push(new Person(name)); > return this.people.last(); > }, > converse: function() { > $A(this.people).each(function(person) { > person.speak(); > }); > } > > }); > > var family = new Family(); > var p1 = family.appendPerson(''John''); > p1.appendToys([ > ''camera'', > ''ipod'' > ]); > var p2 = family.appendPerson(''Jane''); > p2.appendToys([ > ''iphone'', > ''computer'' > ]); > family.converse();--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
To elaborate: this happens because arrays are passed by reference, so in your original code each instance of Person will share the same array of Toys. (They''ll share the same functions, too, but that''s how it''s supposed to work.) Moving the declaration to `initialize` means that each instance creates its own `toys` array on instantiation. You''re not slow; it''s a pothole we''ve all tripped over at least once. Cheers, Andrew On Jan 10, 1:49 pm, itynd...-ue636x8T32g@public.gmane.org wrote:> You need to move "toys: []" in your person class to the initialize > method: > > var Person = Class.create({ > initialize: function(name) { > this.name = name; > this.toys = []; > }, > speak: function() { > var string = "My name is " + this.name + ". My toys > are: "; > $A(this.toys).each(function (toy) { > string += toy.name + ","; > }.bind(this)); > > alert(string); > }, > appendToys: function(items) { > $A(items).each(function (item) { > this.toys.push(new Toy(item)); > }.bind(this)); > } > > }); > > On Jan 10, 2:11 pm, iporter <ispor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m obviously a bit slow - I''m afraid I can''t get the below code to > > work as expected. I''ve changed the concepts to family, people & toys > > to simplify it, but no luck. I would expect the code to make two > > alerts: > > > - My name is John. My toys are: camera,ipod, > > - My name is Jane. My toys are: iphone,computer, > > > However, while the peoples'' names are listed appropriately, it gives > > all the toys to both people: > > > - My name is John. My toys are: camera,ipod,iphone,computer, > > - My name is Jane. My toys are: camera,ipod,iphone,computer, > > > Why is this? Many thanks, > > Iain > > > --------------------------------------------------------------------------- --------------------------- > > var Toy = Class.create({ > > initialize:function(name) { > > this.name = name; > > } > > > }); > > > var Person = Class.create({ > > toys: [], > > initialize: function(name) { > > this.name = name; > > }, > > speak: function() { > > var string = "My name is " + this.name + ". My toys are: "; > > $A(this.toys).each(function (toy) { > > string += toy.name + ","; > > }.bind(this)); > > > alert(string); > > }, > > appendToys: function(items) { > > $A(items).each(function (item) { > > this.toys.push(new Toy(item)); > > }.bind(this)); > > }}); > > > var Family = Class.create({ > > people:[], > > appendPerson: function(name) { > > this.people.push(new Person(name)); > > return this.people.last(); > > }, > > converse: function() { > > $A(this.people).each(function(person) { > > person.speak(); > > }); > > } > > > }); > > > var family = new Family(); > > var p1 = family.appendPerson(''John''); > > p1.appendToys([ > > ''camera'', > > ''ipod'' > > ]); > > var p2 = family.appendPerson(''Jane''); > > p2.appendToys([ > > ''iphone'', > > ''computer'' > > ]); > > family.converse();--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Cheers guys!!! Awesome, and thanks for the explanation Andrew, otherwise I would have just been stumbling along. Very chuffed now :0) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---