Hi, Within the function called by #each, by default the context (the "this" value) is a reference to the global object ('window', on web browsers), not the same context as the code calling #each. This is how JavaScript functions work: Unless you do something to explicitly set "this" when calling them, they default to "this" being the global object. More on this in this blog entry[1].
Because of that, #each has a second parameter[2] that sets the context to use when calling the function. So this would work: var Deletr = Class.create({ initialize: function(options) { this.list = new Array(); $$('a.deletr').each(function(element) { this.list.push(element); }, this); } }); All I did there was add the second parameter to #each. BTW, slightly OT, but you could replace that entire loop with this line of code: this.list = $$('a.deletr'); Unlike DOM methods, Prototype's various DOM wrappers return arrays, not live lists, so you don't have to make a copy. If you really did want to make a copy for some reason, you could do that via the $A function[3] rather than an #each loop. [1] http://blog.niftysnippets.org/2008/04/you-must-remember-this.html [2] http://prototypejs.org/api/enumerable/each [3] http://prototypejs.org/api/utility/dollar-a HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Jul 7, 10:50 pm, David Behler <d.beh...@gmail.com> wrote: > Why does this work: > var Deletr = Class.create({ > initialize: function(options) { > var list = new Array(); > $$('a.deletr').each(function(element) { > list.push(element); > }); > } > > }); > > But this does not? > var Deletr = Class.create({ > initialize: function(options) { > this.list = new Array(); > $$('a.deletr').each(function(element) { > this.list.push(element); > }); > } > > }); > > Firebug reports: > this.list is undefined > this.list.push(element); > > Is there a way to access class attributes when I'm using each to > iterate over an array? > That would be really helpful! > > Thanks > David --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---