John Cartwright
2007-Oct-04 19:15 UTC
prototype: access to instance properties w/in iterator function
Hello All, I''m a little confused about how I can access instance properties w/in an iterator function. function Person(lastName, firstName) { this.lastName = lastName; this.firstName = firstName; this.roles = new Array(); $w(''developer manager'').each(function(i) { //ERROR - "this.roles has no properties" this.roles.push(i) }); } Can someone help me w/ this? Thanks! -- john --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
RobG
2007-Oct-04 21:28 UTC
Re: prototype: access to instance properties w/in iterator function
On Oct 5, 5:15 am, John Cartwright <john.c.cartwri...-Wuw85uim5zDR7s880joybQ@public.gmane.org> wrote:> Hello All, > > I''m a little confused about how I can access instance properties w/in an > iterator function. > function Person(lastName, firstName) { > this.lastName = lastName; > this.firstName = firstName; > this.roles = new Array();var foo = this;> $w(''developer manager'').each(function(i) {To save a needless call to $w() consider: [''developer'', ''manager''].each(function(i) { or even: this.roles = [''developer'', ''manager'']; and save yourself some grief ... but for the sake of the exercise let''s keep going. :-)> //ERROR - "this.roles has no properties" > this.roles.push(i)foo.roles.push(i);> }); > }Why? The anonymous function passed to the each method as access to all the variables of the "outer" Person function. However, each function object has its own this keyword that (usually) references the object that it is called as a method of. If it isn''t a method of some object, its this keyword will reference the window object. The anonymous function is called directly, not as say Person.foo, so its this keyword will reference the global object. So before calling the function, create a local Person variable that references whatever this references (the new Person object if it''s been called as a constructor) so that the anonymous function has access to the right object. -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tom Gregory
2007-Oct-04 21:49 UTC
Re: prototype: access to instance properties w/in iterator function
$w(''developer manager'').each(function(i) { this.roles.push(i) }.bind(this)); // <-- Add bind call (old way, still works) OR $w(''developer manager'').each(function(i) { this.roles.push(i) }, this); // <-- Pass context parameter (new way, requires recent vers of Prototype) TAG On Oct 4, 2007, at 1:15 PM, John Cartwright wrote:> > Hello All, > > I''m a little confused about how I can access instance properties w/ > in an > iterator function. > function Person(lastName, firstName) { > this.lastName = lastName; > this.firstName = firstName; > this.roles = new Array(); > $w(''developer manager'').each(function(i) { > //ERROR - "this.roles has no properties" > this.roles.push(i) > }); > } > > > Can someone help me w/ this? > > Thanks! > > -- john > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tobie Langel
2007-Oct-04 22:06 UTC
Re: prototype: access to instance properties w/in iterator function
I''m not sure what you are trying to do here... but wouldn''t this work ? function Person(lastName, firstName) { this.lastName = lastName; this.firstName = firstName; this.roles = $w(''developer manager''); } --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
John Cartwright
2007-Oct-05 04:56 UTC
Re: prototype: access to instance properties w/in iterator function
Perfect! I realize the example was a little contrived, but your explanation made perfect sense. Thanks for the prompt response. --john On Oct 4, 2007, at 3:28 PM, RobG wrote:> > > > On Oct 5, 5:15 am, John Cartwright <john.c.cartwri...-Wuw85uim5zDR7s880joybQ@public.gmane.org> > wrote: >> Hello All, >> >> I''m a little confused about how I can access instance properties w/ >> in an >> iterator function. >> function Person(lastName, firstName) { >> this.lastName = lastName; >> this.firstName = firstName; >> this.roles = new Array(); > > var foo = this; > > >> $w(''developer manager'').each(function(i) { > > To save a needless call to $w() consider: > > [''developer'', ''manager''].each(function(i) { > > or even: > > this.roles = [''developer'', ''manager'']; > > and save yourself some grief ... but for the sake of the exercise > let''s keep going. :-) > >> //ERROR - "this.roles has no properties" >> this.roles.push(i) > > foo.roles.push(i); > > >> }); >> } > > Why? The anonymous function passed to the each method as access to all > the variables of the "outer" Person function. However, each function > object has its own this keyword that (usually) references the object > that it is called as a method of. If it isn''t a method of some > object, its this keyword will reference the window object. > > The anonymous function is called directly, not as say Person.foo, so > its this keyword will reference the global object. > > So before calling the function, create a local Person variable that > references whatever this references (the new Person object if it''s > been called as a constructor) so that the anonymous function has > access to the right object. > > > -- > Rob > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
John Cartwright
2007-Oct-05 05:03 UTC
Re: prototype: access to instance properties w/in iterator function
Very cool! - Thanks for your help. --john On Oct 4, 2007, at 3:49 PM, Tom Gregory wrote:> > $w(''developer manager'').each(function(i) { > this.roles.push(i) > }.bind(this)); // <-- Add bind call (old way, still works) > > OR > > $w(''developer manager'').each(function(i) { > this.roles.push(i) > }, this); // <-- Pass context parameter (new way, requires > recent vers of Prototype) > > TAG > > On Oct 4, 2007, at 1:15 PM, John Cartwright wrote: > >> >> Hello All, >> >> I''m a little confused about how I can access instance properties w/ >> in an >> iterator function. >> function Person(lastName, firstName) { >> this.lastName = lastName; >> this.firstName = firstName; >> this.roles = new Array(); >> $w(''developer manager'').each(function(i) { >> //ERROR - "this.roles has no properties" >> this.roles.push(i) >> }); >> } >> >> >> Can someone help me w/ this? >> >> Thanks! >> >> -- john >> >>> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---