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 <[EMAIL PROTECTED]> 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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to