Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with prototype library

 
Sapna Grover
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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).
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Christophe Porteneuve
Author
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic