PhilBo
2007-Nov-28 21:12 UTC
Explicit extension of array prototype with Enumerable methods
Background: As is well known, some code is broken by the useful extensions that prototype.js adds to the Array prototype. In particular the "for (var x in array)" idiom that some libraries like to use. (Due to design flaw in Javascript itself, the for loop iterates not only over the elements of the array but also the non-built in functions added by prototype. This breaks code that doesn''t expect functions.) I''m happy to rewrite my code to use the prototype idioms, but I can''t rewrite every library I might want to call that uses the broken idiom. It''s a fairly simple matter to modify prototype.js so that, rather than modifying the Array.prototype, individual arrays are extended explicitly by the $A function only when I want them to. I get the extended array behavior when I want it, and can use vanilla arrays when I call out to third party code. It seems like enough people might want this behavior that it couldbe added as an option on the core prototype.js-- add some sort of switch so that the auto-extend behavior on Arrays can be turned off in favor of the explicit model described here. --~--~---------~--~----~------------~-------~--~----~ 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-Nov-28 22:29 UTC
Re: Explicit extension of array prototype with Enumerable methods
Hi, Andrew posted an excellent write-up on this issue: http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/ and it''s been discussed numerous times in the mailing list already. Adding such an option to Prototype isn''t planned and probably never will, as mistakenly using for...in loop to iterate over array is a practice that''s thankfully disappearing. Best, Tobie On Nov 28, 10:12 pm, PhilBo <philbo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Background: As is well known, some code is broken by the useful > extensions that prototype.js adds to the Array prototype. In > particular the "for (var x in array)" idiom that some libraries like > to use. > (Due to design flaw in Javascript itself, the for loop iterates not > only over the elements of the array but also the non-built in > functions added by prototype. This breaks code that doesn''t expect > functions.) > > I''m happy to rewrite my code to use the prototype idioms, but I can''t > rewrite every library I might want to call that uses the broken idiom. > > It''s a fairly simple matter to modify prototype.js so that, rather > than modifying the Array.prototype, individual arrays are extended > explicitly by the $A function only when I want them to. I get the > extended array behavior when I want it, and can use vanilla arrays > when I call out to third party code. > > It seems like enough people might want this behavior that it couldbe > added as an option on the core prototype.js-- add some sort of switch > so that the auto-extend behavior on Arrays can be turned off in favor > of the explicit model described here.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
PhilBo
2007-Dec-01 01:17 UTC
Re: Explicit extension of array prototype with Enumerable methods
I appreciate these arguments. However, this is makes it difficult for Firefox toolbar authors to use prototype. Popular toolbars like the Facebook toolbar do in fact use the "for (p in a)" idiom. Because all toolbars in Mozilla share a common Array prototype, this means that any toolbar that uses prototype will break such toolbars. You could argue that this in part a design flaw of Mozilla that toolbars are not more isolated from one another, but there''s not too much I can do about that. On Nov 28, 2:29 pm, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Andrew posted an excellent write-up on this issue: > > http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-... > > and it''s been discussed numerous times in the mailing list already. > > Adding such an option to Prototype isn''t planned and probably never > will, as mistakenly using for...in loop to iterate over array is a > practice that''s thankfully disappearing. > > Best, > > Tobie > > On Nov 28, 10:12 pm, PhilBo <philbo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Background: As is well known, some code is broken by the useful > > extensions that prototype.js adds to the Array prototype. In > > particular the "for (var x in array)" idiom that some libraries like > > to use. > > (Due to design flaw in Javascript itself, the for loop iterates not > > only over the elements of the array but also the non-built in > > functions added by prototype. This breaks code that doesn''t expect > > functions.) > > > I''m happy to rewrite my code to use the prototype idioms, but I can''t > > rewrite every library I might want to call that uses the broken idiom. > > > It''s a fairly simple matter to modify prototype.js so that, rather > > than modifying the Array.prototype, individual arrays are extended > > explicitly by the $A function only when I want them to. I get the > > extended array behavior when I want it, and can use vanilla arrays > > when I call out to third party code. > > > It seems like enough people might want this behavior that it couldbe > > added as an option on the core prototype.js-- add some sort of switch > > so that the auto-extend behavior on Arrays can be turned off in favor > > of the explicit model described here.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---