Hello, We have a problem that is related to what is described on http://prototypejs.org/api/array . We have added prototype.js into our product and just adding it breaks existing code which relies on associative arrays. We can change some of the arrays to follow the prototype syntax. However, I m wondering whether there is a work around to this problem especially because there is customer code that we can''t change. Also with porotype our application won''t work in MashUps. I am sure this is not the first time some has encountered this problem. This is really a blocker for us right now. Any help or input is highly appreciated. Thanks Sapna Grover --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
What syntax does prototype break? On 5/30/07, Sapna Grover <sapna.grover-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello, We have a problem that is related to what is described on > http://prototypejs.org/api/array . > We have added prototype.js into our product and just adding it breaks > existing code which relies on associative arrays. > > We can change some of the arrays to follow the prototype syntax. > However, I m wondering whether there is a work around to this problem > especially because there is customer code that we can''t change. Also > with porotype our application won''t work in MashUps. > > I am sure this is not the first time some has encountered this > problem. This is really a blocker for us right now. Any help or input > is highly appreciated. > > Thanks > Sapna Grover > > > > >-- Jesse E.I. Farmer e: jesse-h8Qh2m8E5SHQT0dZR+AlfA@public.gmane.org w: http://20bits.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
function testCase1(){ var array = ["A","B","C"]; var ret = ""; for(var i in array){ ret += array[i] + " "; } alert(ret); } function testCase2(){ var array = ["A","B","C"]; var ret = ""; for(var i = 0;i< array.length;i++){ ret += array[i] + " "; } alert(ret); } function testCase3(){ var array = []; array["a"] = "A"; array["b"] = "B"; array["c"] = "C"; var ret = ""; for(var i in array){ ret += array[i] + " "; } alert(ret); } Test Case 1 and 3 fail when prototype is included. Sapna On May 30, 1:02 pm, "Jesse Farmer" <farme...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What syntax does prototype break? > > On 5/30/07, Sapna Grover <sapna.gro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hello, We have a problem that is related to what is described on > >http://prototypejs.org/api/array. > > We have added prototype.js into our product and just adding it breaks > > existing code which relies on associative arrays. > > > We can change some of the arrays to follow the prototype syntax. > > However, I m wondering whether there is a work around to this problem > > especially because there is customer code that we can''t change. Also > > with porotype our application won''t work in MashUps. > > > I am sure this is not the first time some has encountered this > > problem. This is really a blocker for us right now. Any help or input > > is highly appreciated. > > > Thanks > > Sapna Grover > > -- > Jesse E.I. Farmer > e: j...-h8Qh2m8E5SHQT0dZR+AlfA@public.gmane.org > w:http://20bits.com--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
i think using ''for in'' statement in javascript arrays is actually very bad practice; it should actually be used only on objects there is an explanation (Why you should stop using for...in to iterate (or never take it up)) on this you can find here: http://www.prototypejs.org/api/array best, dasboe On 30 Mai, 19:16, Sapna Grover <sapna.gro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> function testCase1(){ > var array = ["A","B","C"]; > var ret = ""; > for(var i in array){ > ret += array[i] + " "; > } > alert(ret); > } > function testCase2(){ > var array = ["A","B","C"]; > var ret = ""; > for(var i = 0;i< array.length;i++){ > ret += array[i] + " "; > } > alert(ret); > } > function testCase3(){ > var array = []; > array["a"] = "A"; > array["b"] = "B"; > array["c"] = "C"; > var ret = ""; > for(var i in array){ > ret += array[i] + " "; > } > alert(ret); > } > Test Case 1 and 3 fail when prototype is included. > > Sapna > > On May 30, 1:02 pm, "Jesse Farmer" <farme...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > What syntax does prototype break? > > > On 5/30/07, Sapna Grover <sapna.gro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, We have a problem that is related to what is described on > > >http://prototypejs.org/api/array. > > > We have added prototype.js into our product and just adding it breaks > > > existing code which relies on associative arrays. > > > > We can change some of the arrays to follow the prototype syntax. > > > However, I m wondering whether there is a work around to this problem > > > especially because there is customer code that we can''t change. Also > > > with porotype our application won''t work in MashUps. > > > > I am sure this is not the first time some has encountered this > > > problem. This is really a blocker for us right now. Any help or input > > > is highly appreciated. > > > > Thanks > > > Sapna Grover > > > -- > > Jesse E.I. Farmer > > e: j...-h8Qh2m8E5SHQT0dZR+AlfA@public.gmane.org > > w:http://20bits.com--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sapna Grover wrote:> Hello, We have a problem that is related to what is described on > http://prototypejs.org/api/array . > We have added prototype.js into our product and just adding it breaks > existing code which relies on associative arrays. > >When you say "associative arrays" I''m guessing you are referring to the harmful use of storing members as array properties. Andrew Dupont has a very concise article on this practice. http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/ FTA: "Luckily, there is a dead-simple way to fix this. In the above example, you need only change |Array| to |Object|. (Or, if you''re using literal syntax, change |[]| to |{}|.) There. Your wrong code is no longer wrong, and it took only a little more work than a simple find-and-replace."> We can change some of the arrays to follow the prototype syntax. > However, I m wondering whether there is a work around to this problem > especially because there is customer code that we can''t change. Also > with porotype our application won''t work in MashUps. > ... >I would request to the customer that they fix any incorrect usage of Array. Point them to Andrew''s article even. If that doesn''t work, you''ll probably have to use a library that ignores incorrect usage of Array :( Best regards, Ken Snyder --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for your response. In our legacy code we can''t use the object notation to replace the array object because we rely on Array specific methods like push. Should we use Prototype Hash Object ? It doesn''t seem to contain methods to add and remove elements. Any Suggestions? Sapna On May 30, 1:24 pm, Ken Snyder <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sapna Grover wrote: > > Hello, We have a problem that is related to what is described on > >http://prototypejs.org/api/array. > > We have added prototype.js into our product and just adding it breaks > > existing code which relies on associative arrays. > > When you say "associative arrays" I''m guessing you are referring to the > harmful use of storing members as array properties. Andrew Dupont has a > very concise article on this practice. > > http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-... > > FTA: > "Luckily, there is a dead-simple way to fix this. In the above example, > you need only change |Array| to |Object|. (Or, if you''re using literal > syntax, change |[]| to |{}|.) There. Your wrong code is no longer wrong, > and it took only a little more work than a simple find-and-replace." > > > We can change some of the arrays to follow the prototype syntax. > > However, I m wondering whether there is a work around to this problem > > especially because there is customer code that we can''t change. Also > > with porotype our application won''t work in MashUps. > > ... > > I would request to the customer that they fix any incorrect usage of > Array. Point them to Andrew''s article even. If that doesn''t work, > you''ll probably have to use a library that ignores incorrect usage of > Array :( > > Best regards, > > Ken Snyder--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sapna Grover wrote:> Thanks for your response. > In our legacy code we can''t use the object notation to replace the > array object because we rely on Array specific methods like push. > Should we use Prototype Hash Object ? It doesn''t seem to contain > methods to add and remove elements. > Any Suggestions?I don''t think he was suggesting you do that. He was just pointing out that using an Array like an object is just like using Object. Simply put, for..in is not intended to be used for Array, but Object. It loops over *every* property in the *object*. It just so happens that Array is also an Object and by default does not have any extra properties other than the indexed elements. -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
What is your recommendation ? On May 30, 3:34 pm, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> Sapna Grover wrote: > > Thanks for your response. > > In our legacy code we can''t use the object notation to replace the > > array object because we rely on Array specific methods like push. > > Should we use Prototype Hash Object ? It doesn''t seem to contain > > methods to add and remove elements. > > Any Suggestions? > > I don''t think he was suggesting you do that. He was just pointing out that using > an Array like an object is just like using Object. > > Simply put, for..in is not intended to be used for Array, but Object. It loops > over *every* property in the *object*. It just so happens that Array is also an > Object and by default does not have any extra properties other than the indexed > elements. > > -- > Michael Peters > Developer > Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sapna Grover wrote:> What is your recommendation ?Use a normal for loop. var length = array.length; for(var i=0; i< length; i++) { // do stuff } -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I need a data structure to store key/value pair. Thanks Sapna On May 30, 3:52 pm, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> Sapna Grover wrote: > > What is your recommendation ? > > Use a normal for loop. > > var length = array.length; > for(var i=0; i< length; i++) { > // do stuff > > } > > -- > Michael Peters > Developer > Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sapna Grover wrote:> I need a data structure to store key/value pair.Then use Object. Someone is confused here... If you need key value pairs then use Object. If you just need a list of values use Array. You mentioned you needed push/pop. Why? How do you push/pop off a named value? -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It is clear now Thx On May 30, 4:09 pm, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> Sapna Grover wrote: > > I need a data structure to store key/value pair. > > Then use Object. Someone is confused here... If you need key value pairs then > use Object. If you just need a list of values use Array. You mentioned you > needed push/pop. Why? How do you push/pop off a named value? > > -- > Michael Peters > Developer > Plus Three, LP--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On May 31, 7:41 am, Sapna Grover <sapna.gro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> It is clear now ThxWhile using an Array like a plain Object is considered bad coding style in general, there is nothing wrong with it in terms of syntax - it doesn''t break any part of the ECMAScript specification - and there are situations where it can be considered the right thing to do. It is also considered wrong to use $ for variable names in human generated code, yet Prototype.js has no problem with that. It''s also considered bad to extend built-in objects (like Array, String, etc.) - again, Prototype.js has no problem with that either. Up to version 1.5 it extended Object, fortunately the authors saw fit to fix it. A simple solution is don''t use Prototype.js, but I suspect that''s not the answer you''re looking for. :-) Another is to modify Prototype.js''s $A object so that $A is extended, not the built-in Array object. That way you can still get a plain Array when you want and wherever you want a Prototype extended array, intiialise it with: var plainArray = []; var extendedArray = new $A(); or maybe var extendedArray = $A(); or similar. Other libraries seem to be able to do it without any issues so I suspect Prototype.js can too quite easily. -- 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 -~----------~----~----~----~------~----~------~--~---
Am 31.05.2007 um 02:09 schrieb RobG:> While using an Array like a plain Object is considered bad coding > style in general, there is nothing wrong with it in terms of syntax - > it doesn''t break any part of the ECMAScript specification - and there > are situations where it can be considered the right thing to do.No, there is not. You''re iterating over the properties of the object, not the items of the array. It''s not the syntax that''s broken, it''s just plain and clearly wrongly used. Anyway, removing the automatic extension of all arrays will certainly break Prototype from functioning correctly, as it uses arrays internally too, and expects them to be extended. The big thing is: If you have legacy JavaScript code: now it''s a good time to rewrite it, with concise and readable Prototypish syntax and unit tests to boot. :) Best, Thomas --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I think you''re saying the same thing in different ways. I believe Rob is saying that the similarities between array syntax and one way of using object syntax (i.e. square brackets) are similar enough to be confusing if a programmer doesn''t understand the difference. I know you know this, Thomas, but I''m going to go through it anyway for those still learning. Take this object/hash for example: var ob1 = {a: 1, b:2}; It''s clear to us that the most appropriate way to access the properties are ob1.a and ob.2. But consider this object: var ob2 = {''a+2'': 2, ''Hello, world'': 3); We can''t access the properties as "ob2.a+2" That gives us something other than what we want. It is appropriate in this instance to use the square bracket notation: ob2[''a+2''] += 1; Just because we''re using square brackets doesn''t mean we''re dealing with an array. TAG On Jun 2, 2007, at 10:47 AM, Thomas Fuchs wrote:> Am 31.05.2007 um 02:09 schrieb RobG: > >> While using an Array like a plain Object is considered bad coding >> style in general, there is nothing wrong with it in terms of syntax - >> it doesn''t break any part of the ECMAScript specification - and there >> are situations where it can be considered the right thing to do. > > No, there is not. You''re iterating over the properties of the object, > not the items of the array. It''s not the syntax that''s broken, it''s > just plain and clearly wrongly used. > > Anyway, removing the automatic extension of all arrays will certainly > break Prototype from functioning correctly, as it uses arrays > internally too, and expects them to be extended. > > The big thing is: If you have legacy JavaScript code: now it''s a good > time to rewrite it, with concise and readable Prototypish syntax and > unit tests to boot. :) > > Best, > Thomas--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sorry if that came over too harsh, I didn''t mean it that way. :) Best, Thomas Am 02.06.2007 um 19:12 schrieb Tom Gregory:> > I think you''re saying the same thing in different ways. > > I believe Rob is saying that the similarities between array syntax > and one way of using object syntax (i.e. square brackets) are similar > enough to be confusing if a programmer doesn''t understand the > difference. > > I know you know this, Thomas, but I''m going to go through it anyway > for those still learning. > > Take this object/hash for example: > > var ob1 = {a: 1, b:2}; > > It''s clear to us that the most appropriate way to access the > properties are ob1.a and ob.2. But consider this object: > > var ob2 = {''a+2'': 2, ''Hello, world'': 3); > > We can''t access the properties as "ob2.a+2" That gives us something > other than what we want. It is appropriate in this instance to use > the square bracket notation: > > ob2[''a+2''] += 1; > > Just because we''re using square brackets doesn''t mean we''re dealing > with an array. > > > > TAG > > On Jun 2, 2007, at 10:47 AM, Thomas Fuchs wrote: > >> Am 31.05.2007 um 02:09 schrieb RobG: >> >>> While using an Array like a plain Object is considered bad coding >>> style in general, there is nothing wrong with it in terms of >>> syntax - >>> it doesn''t break any part of the ECMAScript specification - and >>> there >>> are situations where it can be considered the right thing to do. >> >> No, there is not. You''re iterating over the properties of the object, >> not the items of the array. It''s not the syntax that''s broken, it''s >> just plain and clearly wrongly used. >> >> Anyway, removing the automatic extension of all arrays will certainly >> break Prototype from functioning correctly, as it uses arrays >> internally too, and expects them to be extended. >> >> The big thing is: If you have legacy JavaScript code: now it''s a good >> time to rewrite it, with concise and readable Prototypish syntax and >> unit tests to boot. :) >> >> Best, >> Thomas > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---