• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue with prototype library

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, adding Prototype to your page should not have effects such as these. Although Prototype is an invasive library (unlike this week's star, jQuery) that actively modifies the definitions of JavaScript objects such as arrays, it should do so in a compatible manner.

Perhaps you could show us a small example of the type of thing that is breaking on your page? Please keep the example small, and be sure to use UBB code tags when posting code.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
Hmmm, adding Prototype to your page should not have effects such as these. Although Prototype is an invasive library (unlike this week's star, jQuery) that actively modifies the definitions of JavaScript objects such as arrays, it should do so in a compatible manner.



I can pretty confirm that this has always been a problem. Maybe its fixed in 1.6 but prototype changes the default behavior of arrays globally when added to a page. It's a PITA. And one more reason why I use JQuery instead of Prototype.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg, what specifically have you seen? I know Prototype adds behavior to Array, but what core behavior no longer works? We use both jQuery and Prototype at my day job and this isn't something we've banged our heads on (yet).
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this referring to the issue that's addressed in the Prototype documentation under the heading "Why you should stop using for�in to iterate (or never take it up)"?
 
Sapna Grover
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this is about the issue with the for .. in loop. What should we do for existing code and in MashUps where there could be such code.

Thanks
Sapna
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. We never use for..in except to enumerate object properties...

If you are using Prototype, you should use its iteration functions to loop over arrays.
 
Author
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sapna,

Oh, that's quite simple. In your code, avoid misusing as an associative array (or hash). JavaScript did not intend it for this purpose, but for integer-indexation storage.

If you need associative arrays / hashes, use what JavaScript gives you: plain objects. In JS, every object acts as an associative array. If you want syntactic sugar over it and a few nifty tricks, since you appear to be using Prototype, use its type (warning: version 1.6 now requires explicit get/set access, much like Java, in order to prevent accidental collisions between the storage of the hash and the numerous mixed-in methods).

As for code you do not have control over (as in, you don't write it, you don't maintain it, and you can't hack its source), well, if they misuse Array and for...in, have the author behave! Or failing that, change libs. Prototype won't change a perfectly legitimate extension just because other developers take an erroneous road and refuse to straighten things up when alerted to the issue :-/

The documentation for Prototype's extensions does explain the issue with code examples. Feel free to ask any further question on the issue here.

'HTH
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christophe Porteneuve:
In your code, avoid misusing Array as an associative array (or hash).


Indeed, this is a pattern propagated by many examples on the web, and by many books. For what reason, I do not know.

Perhaps it's the fact that the general de-referencing operator and the indexing operator are both the square brackets, leading people to believe that somehow an Array is necessary to reference object properties using the [] operator.

Obviously, this is not so.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic