aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Issue with prototype library Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Issue with prototype library" Watch "Issue with prototype library" New topic
Author

Issue with prototype library

Sapna Grover
Ranch Hand

Joined: Sep 27, 2002
Posts: 54
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

Joined: Jan 10, 2002
Posts: 60082
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

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.


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

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
Bartender

Joined: Oct 14, 2005
Posts: 18166
    
    8

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

Joined: Sep 27, 2002
Posts: 54
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

Joined: Jan 10, 2002
Posts: 60082
    
  65

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

Joined: Jan 28, 2008
Posts: 43
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


Christophe Porteneuve aka TDD
"They did not know it was impossible, so they did it." --Mark Twain
Pragmatic Guide to JavaScript: http://books.pragprog.com/titles/pg_js/
Prototype and script.aculo.us: http://books.pragprog.com/titles/cppsu/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with prototype library
 
Similar Threads
Firefox breaks 20,000,000 downloads!
array?
how to write a function to demostrate functionality of 3d in 1d. function prototye:
It Worked!
Doubt in using AJAX with JSP